Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Merge two XML files together based on values

General discussions related to php

Moderators: macek, egami, gesf

Merge two XML files together based on values

Postby mark110384 » Tue Oct 23, 2012 7:18 am

I am working on developing an application that will take 2 xml files and merge into 1. At present I can generate an xml file but it is not formated the way that I want, and I have hit a brick wall at the moment, any help will be greatly appreciated.

The application reads 2 xml files and then goes through each staff member record from one of the xml files. For each staff member record it looks for the corresponding record in the second xml and once it has been discovered it generates a series of new nodes and populates it with the information from the second xml file. I would like to place these new nodes in the the relating staff record at a specific point (at present it just dumps them all at the bottom of the xml document)

The PHP

Code: Select all
<?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

Code: Select all
<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

Code: Select all
<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>


Generated Output

Code: Select all
<?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>


Desired output

Code: Select all
<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>


Any help will be greatly appreciated.
mark110384
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Oct 23, 2012 7:03 am

Return to PHP General

Who is online

Users browsing this forum: southerngomezz and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.