2012-10-23 89 views
0

我正在開發將採用2個XML文件併合併爲1的應用程序。目前,我可以生成XML文件,但它沒有格式化我想要的,現在我已經撞到了一堵磚牆。根據值添加和定位XML節點以生成XML文檔

  1. 該應用程序讀取2個XML文件,然後從一個XML文件中讀取每個員工記錄。
  2. 對於第一個XML文件
    1. 它會在第二個XML
    2. 一旦發現它產生了一系列新的節點,並從第二XML文件中的信息填充它對應的記錄中的每個工作人員記錄

我想放置在有關人員記錄這些新節點在特定點(目前它只是轉儲他們都在XML文檔的底部)

我遇到的問題是從第二個xml中獲取相應的數據,以便與第一個xml文件中的數據一起放置。正如你所看到的,它只是放在文檔的底部,並沒有鏈接到相關記錄(生成輸出),數據應該出現在上面,我不知道目前如何做到這一點,任何建議,將不勝感激。由於

的PHP

<?php 

$schoolExtract = new DOMDocument; 
$schoolExtract->load('schoolExtract.xml'); 

$kcsExtract = new DOMDocument; 
$kcsExtract->preserveWhiteSpace = false; 
$kcsExtract->load('kcsFinalExtract.xml'); 
$xp = new DOMXPath($kcsExtract); 

header("Content-Type: text/plain"); 

$kcsUserRecord = $kcsExtract->getElementsByTagName("StaffDetails"); 

$schoolRecord = $schoolExtract->getElementsByTagName("StaffDetails"); 

$schoolRecordCount = $schoolExtract->getElementsByTagName("SchoolWorkforceMember"); 

$noRecords = $schoolRecordCount->length; 

set_time_limit(0); 

$i = 0; 

foreach($schoolRecord as $row2) 
{ 

    $foundRecord = 0; 

    $NationalInsuranceSE = $row2->getElementsByTagName("NINumber")->item(0)->nodeValue; 
    $StaffMemberNameSE = $row2->getElementsByTagName("PersonFamilyName")->item(0)->nodeValue; 
    $PersonGivenNameSE = $row2->getElementsByTagName("PersonGivenName")->item(0)->nodeValue; 
    $DOBSE = $row2->getElementsByTagName("PersonBirthDate")->item(0)->nodeValue; 

    $newDOBSE = date("d/m/Y", strtotime($DOBSE)); 

    foreach($kcsUserRecord as $row4) 
    { 
     $NationalInsuranceKCS = $row4->getElementsByTagName("NationalInsurance")->item(0)->nodeValue; 
     $StaffMemberNameKCS = $row4->getElementsByTagName("StaffMemberName")->item(0)->nodeValue; 
     $DOBKCS = $row4->getElementsByTagName("PersonBirthDate")->item(0)->nodeValue; 

     if($NationalInsuranceSE == $NationalInsuranceKCS && $StaffMemberNameSE == $StaffMemberNameKCS){ 

      $foundRecord = 1; 

      $xpQuery = $xp->query('/KCSExtract/StaffRecord[StaffDetails/NationalInsurance="'. $NationalInsuranceKCS . '"]'); 

      foreach ($xpQuery as $item) 
      { 

       $newItem = $schoolExtract->documentElement->appendChild(
         $schoolExtract->createElement('ContractOrServiceGroup') 
        ); 

       foreach (array('ContractType', 'ContractStart', 'ContractEnd','Post', 'SchoolArrivalDate', 'DestinationCode', 'Origin', 'LASchoolLevel', 'Estab', 'PostLevelDetails', 'Roles') as $elementName) { 
        $newItem->appendChild(
         $schoolExtract->importNode(
          $item->getElementsByTagName($elementName)->item(0), 
          true 
         ) 
        ); 
       } 

      } 

      $i++;    
     }   

    } 

    if($foundRecord == 1){ 

     /*No Handlers in place yet*/ 
    } 
    else if($foundRecord == 0){ 
     /*No Handlers in place yet*/ 

    } 

} 

$schoolExtract->formatOutput = true; 

echo $schoolExtract->saveXml(); 

?> 

schoolExtract.xml

<Members> 
    <SchoolWorkforceMembers> 
     <SchoolWorkforceMember> 
      <StaffDetails> 
       <TeacherNumber>123456</TeacherNumber> 
       <StaffMemberName> 
        <PersonFamilyName>Wayne</PersonFamilyName> 
        <GivenNames> 
        <GivenName> 
         <PersonGivenName>Bruce</PersonGivenName> 
        </GivenName> 
        </GivenNames> 
       </StaffMemberName> 
       <NINumber>BW123456</NINumber> 
       <GenderCurrent>2</GenderCurrent> 
       <PersonBirthDate>1974-08-07</PersonBirthDate> 
       <Ethnicity>WBRI</Ethnicity> 
       <Disability>NOBT</Disability> 
       <QTStatus>true</QTStatus> 
       <HLTAStatus>false</HLTAStatus> 
      </StaffDetails> 
      <Absences> 
       <Absence> 
        <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
        <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
        <WorkingDaysLost>033.0</WorkingDaysLost> 
        <AbsenceCategory>SIC</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       <Absence> 
        <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
        <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
        <AbsenceCategory>OTH</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       </Absences> 
     </SchoolWorkforceMember> 
     <SchoolWorkforceMember> 
      <StaffDetails> 
       <TeacherNumber>123456</TeacherNumber> 
       <StaffMemberName> 
        <PersonFamilyName>Parker</PersonFamilyName> 
        <GivenNames> 
        <GivenName> 
         <PersonGivenName>Peter</PersonGivenName> 
        </GivenName> 
        </GivenNames> 
       </StaffMemberName> 
       <NINumber>SM123456</NINumber> 
       <GenderCurrent>2</GenderCurrent> 
       <PersonBirthDate>1974-08-07</PersonBirthDate> 
       <Ethnicity>WBRI</Ethnicity> 
       <Disability>NOBT</Disability> 
       <QTStatus>true</QTStatus> 
       <HLTAStatus>false</HLTAStatus> 
      </StaffDetails> 
      <Absences> 
       <Absence> 
        <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
        <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
        <WorkingDaysLost>033.0</WorkingDaysLost> 
        <AbsenceCategory>SIC</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       <Absence> 
        <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
        <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
        <AbsenceCategory>OTH</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       </Absences> 
     </SchoolWorkforceMember> 
    </SchoolWorkforceMembers> 
</Members> 

kcsFinalExtract.xml

<KCSExtract> 
    <StaffRecord> 
    <StaffDetails> 
     <NationalInsurance>BW123456</NationalInsurance> 
     <PersonBirthDate>1974-08-07</PersonBirthDate> 
     <StaffMemberName>Wayne</StaffMemberName> 
     <PersonGivenName>Bruce</PersonGivenName> 
    </StaffDetails> 
    <ContractOrServiceGroup> 
     <ContractOrService> 
     <ContractType>PRM</ContractType> 
     <ContractStart>01/09/2003</ContractStart> 
     <ContractEnd></ContractEnd> 
     <Post>TCH</Post> 
     <SchoolArrivalDate>01/12/2007</SchoolArrivalDate> 
     <DestinationCode>TU</DestinationCode> 
     <Origin>NOTKNW</Origin> 
     <LASchoolLevel></LASchoolLevel> 
     <Estab></Estab> 
     <PostLevelDetails> 
      <Payments> 
      <Scale>TU</Scale> 
      <RegionSpine>EW</RegionSpine> 
      <SpinePoint>U2</SpinePoint> 
      <TotalPay>35,447.00</TotalPay> 
      <SafeguardedSalary>FALSE</SafeguardedSalary> 
      </Payments> 
      <Hours> 
      <Hours>32.5</Hours> 
      <FTEHours>32.5</FTEHours> 
      <WeeksPerYear>52</WeeksPerYear> 
      </Hours> 
     </PostLevelDetails> 
     <Roles> 
      <Role> 
      <RoleIdentifier>TCHR</RoleIdentifier> 
      </Role> 
     </Roles> 
     </ContractOrService> 
    </ContractOrServiceGroup> 
    </StaffRecord> 
    <StaffRecord> 
    <StaffDetails> 
     <NationalInsurance>SM123456</NationalInsurance> 
     <PersonBirthDate>03/01/1971</PersonBirthDate> 
     <StaffMemberName>Parker</StaffMemberName> 
     <PersonGivenName>Peter</PersonGivenName> 
    </StaffDetails> 
    <ContractOrServiceGroup> 
     <ContractOrService> 
     <ContractType>PRM</ContractType> 
     <ContractStart>01/09/2003</ContractStart> 
     <ContractEnd></ContractEnd> 
     <Post>TCH</Post> 
     <SchoolArrivalDate>01/12/2007</SchoolArrivalDate> 
     <DestinationCode>TU</DestinationCode> 
     <Origin>NOTKNW</Origin> 
     <LASchoolLevel></LASchoolLevel> 
     <Estab></Estab> 
     <PostLevelDetails> 
      <Payments> 
      <Scale>TU</Scale> 
      <RegionSpine>EW</RegionSpine> 
      <SpinePoint>U2</SpinePoint> 
      <TotalPay>35,447.00</TotalPay> 
      <SafeguardedSalary>FALSE</SafeguardedSalary> 
      </Payments> 
      <Hours> 
      <Hours>32.5</Hours> 
      <FTEHours>32.5</FTEHours> 
      <WeeksPerYear>52</WeeksPerYear> 
      </Hours> 
     </PostLevelDetails> 
     <Roles> 
      <Role> 
      <RoleIdentifier>TCHR</RoleIdentifier> 
      </Role> 
     </Roles> 
     </ContractOrService> 
    </ContractOrServiceGroup> 
    </StaffRecord> 
    </KCSExtract> 

生成的輸出

<?xml version="1.0"?> 
<Members> 
    <SchoolWorkforceMembers> 
     <SchoolWorkforceMember> 
      <StaffDetails> 
       <TeacherNumber>123456</TeacherNumber> 
       <StaffMemberName> 
        <PersonFamilyName>Wayne</PersonFamilyName> 
        <GivenNames> 
        <GivenName> 
         <PersonGivenName>Bruce</PersonGivenName> 
        </GivenName> 
        </GivenNames> 
       </StaffMemberName> 
       <NINumber>BW123456</NINumber> 
       <GenderCurrent>2</GenderCurrent> 
       <PersonBirthDate>1974-08-07</PersonBirthDate> 
       <Ethnicity>WBRI</Ethnicity> 
       <Disability>NOBT</Disability> 
       <QTStatus>true</QTStatus> 
       <HLTAStatus>false</HLTAStatus> 
      </StaffDetails> 
      <Absences> 
       <Absence> 
        <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
        <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
        <WorkingDaysLost>033.0</WorkingDaysLost> 
        <AbsenceCategory>SIC</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       <Absence> 
        <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
        <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
        <AbsenceCategory>OTH</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       </Absences> 
     </SchoolWorkforceMember> 
     <SchoolWorkforceMember> 
      <StaffDetails> 
       <TeacherNumber>123456</TeacherNumber> 
       <StaffMemberName> 
        <PersonFamilyName>Parker</PersonFamilyName> 
        <GivenNames> 
        <GivenName> 
         <PersonGivenName>Peter</PersonGivenName> 
        </GivenName> 
        </GivenNames> 
       </StaffMemberName> 
       <NINumber>SM123456</NINumber> 
       <GenderCurrent>2</GenderCurrent> 
       <PersonBirthDate>1974-08-07</PersonBirthDate> 
       <Ethnicity>WBRI</Ethnicity> 
       <Disability>NOBT</Disability> 
       <QTStatus>true</QTStatus> 
       <HLTAStatus>false</HLTAStatus> 
      </StaffDetails> 
      <Absences> 
       <Absence> 
        <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
        <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
        <WorkingDaysLost>033.0</WorkingDaysLost> 
        <AbsenceCategory>SIC</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       <Absence> 
        <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
        <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
        <AbsenceCategory>OTH</AbsenceCategory> 
        <Estab>6905</Estab> 
       </Absence> 
       </Absences> 
     </SchoolWorkforceMember> 
    </SchoolWorkforceMembers> 
<ContractOrServiceGroup><ContractType>PRM</ContractType><ContractStart>01/09/2003</ContractStart><ContractEnd/><Post>TCH</Post><SchoolArrivalDate>01/12/2007</SchoolArrivalDate><DestinationCode>TU</DestinationCode><Origin>NOTKNW</Origin><LASchoolLevel/><Estab/><PostLevelDetails><Payments><Scale>TU</Scale><RegionSpine>EW</RegionSpine><SpinePoint>U2</SpinePoint><TotalPay>35,447.00</TotalPay><SafeguardedSalary>FALSE</SafeguardedSalary></Payments><Hours><Hours>32.5</Hours><FTEHours>32.5</FTEHours><WeeksPerYear>52</WeeksPerYear></Hours></PostLevelDetails><Roles><Role><RoleIdentifier>TCHR</RoleIdentifier></Role></Roles></ContractOrServiceGroup><ContractOrServiceGroup><ContractType>PRM</ContractType><ContractStart>01/09/2003</ContractStart><ContractEnd/><Post>TCH</Post><SchoolArrivalDate>01/12/2007</SchoolArrivalDate><DestinationCode>TU</DestinationCode><Origin>NOTKNW</Origin><LASchoolLevel/><Estab/><PostLevelDetails><Payments><Scale>TU</Scale><RegionSpine>EW</RegionSpine><SpinePoint>U2</SpinePoint><TotalPay>35,447.00</TotalPay><SafeguardedSalary>FALSE</SafeguardedSalary></Payments><Hours><Hours>32.5</Hours><FTEHours>32.5</FTEHours><WeeksPerYear>52</WeeksPerYear></Hours></PostLevelDetails><Roles><Role><RoleIdentifier>TCHR</RoleIdentifier></Role></Roles></ContractOrServiceGroup></Members> 

所需的輸出

<Members> 
    <SchoolWorkforceMembers> 
    <SchoolWorkforceMember> 
     <StaffDetails> 
     <TeacherNumber>123456</TeacherNumber> 
     <StaffMemberName> 
      <PersonFamilyName>Wayne</PersonFamilyName> 
      <GivenNames> 
      <GivenName> 
       <PersonGivenName>Bruce</PersonGivenName> 
      </GivenName> 
      </GivenNames> 
     </StaffMemberName> 
     <NINumber>BW123456</NINumber> 
     <GenderCurrent>2</GenderCurrent> 
     <PersonBirthDate>1974-08-07</PersonBirthDate> 
     <Ethnicity>WBRI</Ethnicity> 
     <Disability>NOBT</Disability> 
     <QTStatus>true</QTStatus> 
     <HLTAStatus>false</HLTAStatus> 
     </StaffDetails> 
     <ContractOrServiceGroup> 
     <ContractOrService> 
      <ContractType>PRM</ContractType> 
      <ContractStart>01/09/2003</ContractStart> 
      <ContractEnd></ContractEnd> 
      <Post>TCH</Post> 
      <SchoolArrivalDate>01/12/2007</SchoolArrivalDate> 
      <DestinationCode>TU</DestinationCode> 
      <Origin>NOTKNW</Origin> 
      <LASchoolLevel></LASchoolLevel> 
      <Estab></Estab> 
      <PostLevelDetails> 
      <Payments> 
       <Scale>TU</Scale> 
       <RegionSpine>EW</RegionSpine> 
       <SpinePoint>U2</SpinePoint> 
       <TotalPay>35,447.00</TotalPay> 
       <SafeguardedSalary>FALSE</SafeguardedSalary> 
      </Payments> 
      <Hours> 
       <Hours>32.5</Hours> 
       <FTEHours>32.5</FTEHours> 
       <WeeksPerYear>52</WeeksPerYear> 
      </Hours> 
      </PostLevelDetails> 
      <Roles> 
      <Role> 
       <RoleIdentifier>TCHR</RoleIdentifier> 
      </Role> 
      </Roles> 
     </ContractOrService> 
     </ContractOrServiceGroup> 
     <Absences> 
     <Absence> 
      <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
      <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
      <WorkingDaysLost>033.0</WorkingDaysLost> 
      <AbsenceCategory>SIC</AbsenceCategory> 
      <Estab>6905</Estab> 
     </Absence> 
     <Absence> 
      <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
      <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
      <AbsenceCategory>OTH</AbsenceCategory> 
      <Estab>6905</Estab> 
     </Absence> 
     </Absences> 
    </SchoolWorkforceMember> 
    <SchoolWorkforceMember> 
     <StaffDetails> 
     <TeacherNumber>123456</TeacherNumber> 
     <StaffMemberName> 
      <PersonFamilyName>Parker</PersonFamilyName> 
      <GivenNames> 
      <GivenName> 
       <PersonGivenName>Peter</PersonGivenName> 
      </GivenName> 
      </GivenNames> 
     </StaffMemberName> 
     <NINumber>SM123456</NINumber> 
     <GenderCurrent>2</GenderCurrent> 
     <PersonBirthDate>1974-08-07</PersonBirthDate> 
     <Ethnicity>WBRI</Ethnicity> 
     <Disability>NOBT</Disability> 
     <QTStatus>true</QTStatus> 
     <HLTAStatus>false</HLTAStatus> 
     </StaffDetails> 
     <ContractOrServiceGroup> 
     <ContractOrService> 
      <ContractType>PRM</ContractType> 
      <ContractStart>01/09/2003</ContractStart> 
      <ContractEnd></ContractEnd> 
      <Post>TCH</Post> 
      <SchoolArrivalDate>01/12/2007</SchoolArrivalDate> 
      <DestinationCode>TU</DestinationCode> 
      <Origin>NOTKNW</Origin> 
      <LASchoolLevel></LASchoolLevel> 
      <Estab></Estab> 
      <PostLevelDetails> 
      <Payments> 
       <Scale>TU</Scale> 
       <RegionSpine>EW</RegionSpine> 
       <SpinePoint>U2</SpinePoint> 
       <TotalPay>35,447.00</TotalPay> 
       <SafeguardedSalary>FALSE</SafeguardedSalary> 
      </Payments> 
      <Hours> 
       <Hours>32.5</Hours> 
       <FTEHours>32.5</FTEHours> 
       <WeeksPerYear>52</WeeksPerYear> 
      </Hours> 
      </PostLevelDetails> 
      <Roles> 
      <Role> 
       <RoleIdentifier>TCHR</RoleIdentifier> 
      </Role> 
      </Roles> 
     </ContractOrService> 
     </ContractOrServiceGroup> 
     <Absences> 
     <Absence> 
      <FirstDayOfAbsence>2011-09-05</FirstDayOfAbsence> 
      <LastDayOfAbsence>2011-10-30</LastDayOfAbsence> 
      <WorkingDaysLost>033.0</WorkingDaysLost> 
      <AbsenceCategory>SIC</AbsenceCategory> 
      <Estab>6905</Estab> 
     </Absence> 
     <Absence> 
      <FirstDayOfAbsence>2012-01-17</FirstDayOfAbsence> 
      <LastDayOfAbsence>2012-01-17</LastDayOfAbsence> 
      <AbsenceCategory>OTH</AbsenceCategory> 
      <Estab>6905</Estab> 
     </Absence> 
     </Absences> 
    </SchoolWorkforceMember> 
    </SchoolWorkforceMembers> 
</Members> 

回答

0

您正在創建一個新的容器元素ContractOrServiceGroup,然後將其附加到文檔的根元素,然後追加到它更多的元素。

  $newItem = $schoolExtract->documentElement->appendChild(
        $schoolExtract->createElement('ContractOrServiceGroup') 
       ); 

您應該做的是創建新元素及其所有子元素,然後將其附加到文檔的適當位置。

例如:

  $newItem = $schoolExtract->createElement('ContractOrServiceGroup'); 

      foreach (array('ContractType', 'etc')) { 
       // build $newItem tree 
      } 

      // $row2 I think is the StaffDetails element in schoolExtract.xml? 

      // If so, this will add to the end of StaffDetails's parent: 
      $row2->parentNode->appendChild($newItem); 

      // This will add to the beginning: 
      $row2->parentNode->insertBefore($newItem, $row2->parentNode->firstChild); 

      // This will add it before StaffDetails: 
      $row2->parentNode->insertBefore($newItem, $row2); 

      // This will add it after: 
      $row2->parentNode->insertBefore($newItem, $row2->nextSibling); 

此代碼是非常難追。你有沒有考慮過使用SimpleXML?這是一個更清晰的數據XML API,就像你正在使用的那樣。

+0

不錯的一個我做它正是我想要它做的。謝謝弗朗西斯,你是男人。 – user1463550