January 17th, 2012, 07:07 AM
-
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 &$xml1, SimpleXMLElement $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
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
January 22nd, 2012, 01:33 PM
-
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 &$xml1, SimpleXMLElement $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
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?