문서에서 특정 XML 데이터를 선택하는 데 문제가 있습니다. 기본 데이터는 마케팅 이벤트입니다. 문서 당 여러 개의 이벤트가있을 수 있습니다. 각 이벤트에는 여러 명의 참석자와 등록자가 있습니다. foreach
루프 안에 SelectNodes()
을 사용하여 시작한 다음이를 CSV로 변환하기 전에 해시 테이블로 읽습니다.특정 노드 속성 선택
단일 이벤트의 경우에는 정상적으로 작동하는 것처럼 보였지만 여러 이벤트의 경우 행이 일관되지 않았기 때문에 eventid가 다른 레코드 데이터와 동기화되지 않았습니다. 이제 전체 XML을 CSV로 내보내고 ETL 도구가 거기에서 제어 할 수 있도록 할 생각입니다.
여기서 필자가 이해할 수없는 부분이 있으며, 여러 특정 XML 속성을 CSV로 선택하는 방법을 알고 있지만 순서가 유지되어 있는지 궁금해했습니다.
내 PowerShell을 코드 :
cls
[xml]$xml = Get-Content ("D:\sample.xml")
$dataTable = @()
$eventNodes = $xml.SelectNodes('//event')
foreach ($event in $eventNodes) {
$eventid = $event.eventid
$eventtitle = $event.eventtitle.InnerText
$eventtime = $event.eventtime
# get registrant data
$registrantNodes = $xml.SelectNodes('//registrant')
foreach ($registrant in $registrantNodes) {
$firstname = $registrant.firstname.InnerText
$lastname = $registrant.lastname.InnerText
$city = $registrant.city.InnerText
$state = $registrant.state.InnerText
$country = $registrant.country.InnerText
$company = $registrant.company.InnerText
$workphone = $registrant.workphone.InnerText
$email = $registrant.email.InnerText
# get attendee data
$attendeeNodes = $xml.SelectNodes('//attendee')
foreach ($attendee in $attendeeNodes) {
$attendedlive = $attendee.attendedlive.InnerText
$attendedarchive = $attendee.attendedarchive.InnerText
# put all data into holding table
$dataEntry = New-Object PSObject -Property @{
FirstName = $firstname;
LastName = $lastname;
City = $city;
State = $state;
Country = $country;
Company = $company;
WorkPhone = $workphone;
Email = $email;
AttendedLive = $attendedlive;
AttendedArchive = $attendedarchive;
EventID = $eventid;
EventTitle = $eventtitle;
EventTime = $eventtime;
Orginization = 'North America';
}
$dataTable += $dataEntry
}
}
}
# display holding table
$dataTable
$dataTable | Export-Csv -Force -Path "D:\output.csv" -NoTypeInformation
나는 샘플 XML 파일
here을 업로드했습니다. 레이아웃은 다음과 같습니다
이렇게 XML 파일을로드하지 마십시오. PowerShell에서 XML 파일을로드하는 올바른 방법은'$ xml = New-Object xml; $ xml.Load ($ 경로)'. 이 방법을 사용하면 XML 파일 인코딩을 자동으로 자동 감지합니다. 'Get-Content'를 사용하면 파일 인코딩이'Get-Content'의 기본값과 일치하지 않을 때 데이터가 깨집니다. 이것은 똑똑하지 않은 손가락 교차와 같습니다. – Tomalak