Re: XML Get Value of Node DOMXPath

From: Date: Tue, 16 Dec 2008 14:02:41 +0000
Subject: Re: XML Get Value of Node DOMXPath
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-284659@lists.php.net to get a copy of this message
Stephen Alistoun wrote:
Hello all, Need help to get the value of the node. We know how to get the value of the city , item and itemPrice nodes below. How do we get the value of NumberOfRooms? $hotelElements = $xpath->query( 'xxxx', $searchReponseElement ); foreach( $hotelElements as $hotelElement ) { $city = $xpath->query( 'City' , $hotelElement ); $item = $xpath->query( 'Item' , $hotelElement ); $itemPrice = $xpath->query( 'ItemPrice' , $hotelElement ); $confirmation = $xpath->query( 'Confirmation' , $hotelElement ); } Here is an example of the XML Response: <Hotel HasExtraInfo="true">
		      <City Code="AMS"><![CDATA[ Amsterdam ]]></City>
		      <Item Code="JAN"><![CDATA[ JAN LUYKEN HTL AND RESIDENCE]]></Item>
		      <LocationDetails>
		          <Location Code="G1"><![CDATA[ Central ]]></Location>
		      </LocationDetails>
		      <StarRating>4</StarRating>
             <HotelRooms>
                 <HotelRoom                        Code="SB"                        NumberOfRooms="1" />                     <HotelRoom                        Code="TB"                        ExtraBed="true"                        NumberCots="1"                        NumberOfExtraBeds="2"                              NumberOfRooms="1                                            SharingBedding="true" />                                                 </HotelRooms>
Thanks
You can do it in a number of ways. $rooms = $xpath->query('/Hotel/HotelRooms/HotelRoom'); /* Via DOMElement */ foreach ($rooms AS $room) {
    echo $room->getAttribute('NumberOfRooms') . "\n\n";
} /* Via XPath */ foreach ($rooms AS $room) { $numrooms = $xpath->evaluate('number(./@NumberOfRooms)', $room); echo $numrooms . "\n\n"; } Rob

« previous php.general (#284659) next »