根据值添加和定位XML节点以生成XML文档

时间:2012-10-23 23:10:36

标签: php xml

我正在开发一个应用程序,它将获取2个XML文件并合并为1.目前我可以生成一个XML文件,但它没有按照我想要的方式进行格式化,而且我已经在时刻。

  1. 应用程序读取2个XML文件,然后从其中一个XML文件中查看每个工作人员成员记录。
  2. 对于第一个XML文件中的每个职员记录
    1. 在第二个XML中查找相应的记录
    2. 一旦发现,它会生成一系列新节点,并使用第二个XML文件中的信息填充它
  3. 我想将这些新节点放在特定点的相关人员记录中(目前它只是将它们全部转储到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>
    

1 个答案:

答案 0 :(得分: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。