Reputation: 145
I want to get the value present in xml node for that I written following code.
The code aim is to get
1.Board value,
2.Address->City value,
3.Photo->PropertyPhoto->SequenceId value.
Code is:
//some code
$fsp = $xml->saveXML();
echo $fsp; //it's output is given below
$s = simplexml_import_dom($fsp);
echo $s->PropertyDetails[0]->Board;
echo $s->PropertyDetails[0]->Address->City;
echo $s->PropertyDetails[0]->Photo->PropertyPhoto->SequenceId;
OUTPUT OF echo $fsp is:
<?xml version="1.0" encoding="UTF-8"?>
<PropertyDetails ID="13953882" LastUpdated="Thu, 09 Jan 2014 01:43:48 GMT">
<ListingID>188691</ListingID>
<Board>117</Board>
<Address>
<StreetAddress>B LOCAL RD</StreetAddress>
<AddressLine1>B LOCAL RD</AddressLine1>
<City>PORT REXTON</City>
<Province>Newfoundland & Labrador</Province>
<PostalCode>A0C2H0</PostalCode>
<Country>Canada</Country>
</Address>
<Photo>
<PropertyPhoto>
<SequenceId>1</SequenceId>
</PropertyPhoto>
<PropertyPhoto>
<SequenceId>12</SequenceId>
</PropertyPhoto>
</Photo>
<ViewType>Ocean view</ViewType>
It doesn't given any output.
Output required is:
117
PORT REXTON
1,12
Help me.
Upvotes: 0
Views: 108
Reputation: 723
A couple things I see here. First of all, your XML is not properly formatted. Your PropertyDetails tag is not closed. So close that out, and instead of using a DOMDocument, use the SimpleXMLElement class.
First your XML needs to corrected like thus:
<?xml version="1.0" encoding="UTF-8"?>
<PropertyDetails ID="13953882" LastUpdated="Thu, 09 Jan 2014 01:43:48 GMT">
<ListingID>188691</ListingID>
<Board>117</Board>
<Address>
<StreetAddress>B LOCAL RD</StreetAddress>
<AddressLine1>B LOCAL RD</AddressLine1>
<City>PORT REXTON</City>
<Province>Newfoundland & Labrador</Province>
<PostalCode>A0C2H0</PostalCode>
<Country>Canada</Country>
</Address>
<Photo>
<PropertyPhoto>
<SequenceId>1</SequenceId>
</PropertyPhoto>
<PropertyPhoto>
<SequenceId>12</SequenceId>
</PropertyPhoto>
</Photo>
<ViewType>Ocean view</ViewType>
</PropertyDetails>
Notice the last line.
Now use SimpleXMLElement and you will be close to accessing the way you want.
$s = new SimpleXMLElement($fsp);
echo $s->Board;
echo $s->Address->City;
$sequence_ids = array();
foreach($s->Photo->PropertyPhoto as $PropertyPhoto)
$sequence_ids[] = $PropertyPhoto->SequenceId;
echo implode(',',$sequence_ids);
Since is your root node in the XML, you dont need to access it via ->PropertyDetails[0]. It knows.
Hope this helps.
Upvotes: 1
Reputation: 1296
First load the xml from the string within your var:
$doc = new DOMDocument();
$doc->load($fsp);
apidoc here: http://www.php.net/manual/en/domdocument.loadxml.php
And then read the tags requested:
//board
$boards = $doc->getElementsByTagName('Board');
echo $boards[0]->nodeValue, PHP_EOL;
//city
$cities = $doc->getElementsByTagName('City');
echo $cities[0]->nodeValue, PHP_EOL;
//sequence ids
$arrIds = array();
foreach ($doc->getElementsByTagName('SequenceId') as $sid) $arrIds[] = $sid->nodeValue;
echo implode(',', $arrIds);
This assumes you only have only one listing within your file! Otherwise you have to do the tag-lookups depending on parent-nodes (not on global document).
Hope this helps!
Upvotes: 1