mark110384 Posted October 23, 2012 Share Posted October 23, 2012 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 <?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> Generated Output <?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 <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. Quote Link to comment https://forums.phpfreaks.com/topic/269820-merge-to-xml-files-together-based-on-values/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.