XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 17th, 2012, 07:07 AM
BENABD22 BENABD22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 2 BENABD22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 59 sec
Reputation Power: 0
Combine 2 xml files in php Dom

Hello,

I want to pull all child elements of second XML Hotel tage and import into first XML after the end of the Hotel tag

XML1:
Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="DB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">121.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


XML2:
Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="TB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">135.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


The function im using:
PHP Code:
function simplexml_merge (SimpleXMLElement &$xml1SimpleXMLElement $xml2) {      
// convert SimpleXML objects into DOM ones      
$dom1 = new DomDocument();      
$dom2 = new DomDocument();      
$dom1->loadXML($xml1->asXML());      $dom2->loadXML($xml2->asXML());         
// pull all child elements of second XML      
$xpath = new domXPath($dom2);      
$xpathQuery $xpath->query('ResponseDetails/SearchPriceResponse/HotelDetails/Hotel');      
for(
$i 0$i $xpathQuery->length$i++) 
// and pump them into first one
$dom1->documentElement->appendChild($dom1->importNode($xpathQuery->item($i), true));      
// for($i = 0; $i < $xpathQuery->length; $i++)      
$xml1 simplexml_import_dom($dom1);   
// function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) 


The problem is, when it pumps the child elements into the first XML it is putting them at the end of the file, ie after

Code:
</hotel> 
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


Instead of after

Code:
</hotel>


Im then parsing and grouping the newly created XML into an array for each Hotel.


Alternatively is it possible to just add the second XML to first XML to give the following output:

Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="DB" NumberOfRooms="1"/>
        <HotelRoom Code="TB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
           <Description><![CDATA[Standard]]></Description>
           <ItemPrice Currency="GBP">121.00</ItemPrice>
        </RoomCategory>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">135.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>



Any help would be much appreciated!

Thank you

Reply With Quote
  #2  
Old January 22nd, 2012, 01:33 PM
BENABD22 BENABD22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 2 BENABD22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 59 sec
Reputation Power: 0
Quote:
Originally Posted by BENABD22
Hello,

I want to pull all child elements of second XML Hotel tage and import into first XML after the end of the Hotel tag

XML1:
Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="DB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">121.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


XML2:
Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="TB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">135.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


The function im using:
PHP Code:
function simplexml_merge (SimpleXMLElement &$xml1SimpleXMLElement $xml2) {      
// convert SimpleXML objects into DOM ones      
$dom1 = new DomDocument();      
$dom2 = new DomDocument();      
$dom1->loadXML($xml1->asXML());      $dom2->loadXML($xml2->asXML());         
// pull all child elements of second XML      
$xpath = new domXPath($dom2);      
$xpathQuery $xpath->query('ResponseDetails/SearchPriceResponse/HotelDetails/Hotel');      
for(
$i 0$i $xpathQuery->length$i++) 
// and pump them into first one
$dom1->documentElement->appendChild($dom1->importNode($xpathQuery->item($i), true));      
// for($i = 0; $i < $xpathQuery->length; $i++)      
$xml1 simplexml_import_dom($dom1);   
// function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) 


The problem is, when it pumps the child elements into the first XML it is putting them at the end of the file, ie after

Code:
</hotel> 
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>


Instead of after

Code:
</hotel>


Im then parsing and grouping the newly created XML into an array for each Hotel.


Alternatively is it possible to just add the second XML to first XML to give the following output:

Code:
<ResponseDetails>
 <SearchPriceResponse>
 <HotelDetails>
 <Hotel>
    <HotelRooms>
        <HotelRoom Code="DB" NumberOfRooms="1"/>
        <HotelRoom Code="TB" NumberOfRooms="1"/>
    </HotelRooms>
    <RoomCategories>
        <RoomCategory Id="001:APE">
           <Description><![CDATA[Standard]]></Description>
           <ItemPrice Currency="GBP">121.00</ItemPrice>
        </RoomCategory>
        <RoomCategory Id="001:APE">
            <Description><![CDATA[Standard]]></Description>
            <ItemPrice Currency="GBP">135.00</ItemPrice>
        </RoomCategory>
    </RoomCategories>
 </hotel>
 </hotelDetails>
 </SearchPriceResponse>
 </ResponseDetails>



Any help would be much appreciated!

Thank you



119 views with not a single reply, have i asked the wrong question?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Combine 2 xml files in php Dom

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap