I have the following XML which I need to parse values from.
<ads>
<ad id="987654321">
<price>
<currency-iso-code>
<value localized-label="£">GBP</value>
</currency-iso-code>
<amount>345</amount>
</price>
<price_frequency>
<value>WEEKLY</value>
</price_frequency>
<title>This is the Title</title>
<description>
Description
</description>
<ad_status>
<value>ACTIVE</value>
</ad_status>
<email>EXIST</email>
<user-id>123456</user-id>
<phone>123456</phone>
<modification-date-time>2013-09-02T11:40:41.000+01:00</modification-date-time>
<start-date-time>2013-09-02T11:40:39.000+01:00</start-date-time>
<features_active>
<category id="3">
<id-name>category-name</id-name>
<localized-name>Category name</localized-name>
</category>
<locations>
<location id="10000392">
<id-name>uk</id-name>
<localized-name>United Kingdom</localized-name>
</location>
</locations>
<neighborhood>Neighbourhood Name</neighborhood>
<attributes>
<attribute localized-label="Seller type" type="ENUM" name="seller_type">
<value localized-label="Agency">trade</value>
</attribute>
<attribute localized-label="Property type" type="ENUM" name="property_type">
<value localized-label="Flat">flat</value>
</attribute>
<attribute localized-label="Number of beds" type="LONG" name="property_number_beds">
<value>1</value>
</attribute>
<attribute localized-label="Date available" type="DATETIME" name="available_date">
<value localized-label="15/05/2013">2013-05-15T00:00:00.000+01:00</value>
</attribute>
</attributes>
<link rel="self" href="https://api2.domain.com/api/ads/987654321">
<link rel="self-user" href="https://api2.domain.com/api/users/123456/ads/987654321">
<public_link href="http://domain.com/public_facing_link">
<pictures>
<picture>
<link rel="extrabig" href="http://domain.com/images/80.JPG">
<link rel="preview" href="http://domain.com/images/81.JPG">
<link rel="big" href="http://domain.com/images/79.JPG">
<link rel="thumb" href="http://domain.com/images/78.JPG">
<link rel="moreadsthumb" href="http://domain.com/images/77.JPG">
</picture>
<picture>
<link rel="extrabig" href="http://domain.com/images/80.JPG">
<link rel="preview" href="http://domain.com/images/81.JPG">
<link rel="big" href="http://domain.com/images/79.JPG">
<link rel="thumb" href="http://domain.com/images/78.JPG">
<link rel="moreadsthumb" href="http://domain.com/images/77.JPG">
</picture>
</pictures>
</public_link>
</features_active>
</ad>
</ads>
I need to pull the following data from this xml
Title, Description - easy enoguht to pull. Done them
I also need public_link href and the pictures href link for those with rel="thumb"
Through checking numerous posts on here and php documentation I have come up with something like this.
$ads = simplexml_load_string($xml);
foreach ($ads as $ad) {
$item['price'] = $ad->price->amount;
$item['title'] = $ad->title;
$link = simplexml_load_string($ad->features_active->public_link);
foreach($link->attributes() as $attr => $value) {
if($attr == 'href'){
$item['link'] = $value;
}
}
$pics = simplexml_load_string($ad->features_active->public_link->pictures);
foreach($pics->picture[0]->attributes() as $attr => $value) {
if($attr == 'thumb'){
$item['picture'] = $value;
}
}
echo "<br><br>" . $item['price'];
echo "<br>" . $item['title'];
echo "<br>" . $item['link'];
echo "<br>" . $item['picture'];
}
For some reason it's not wanting to pull the attributes. Can anyone point me in the right direction for this?
Try PHP DOMDocument for XML Parser. Refer the below URL for getting the tag and attribute value.
http://www.php.net/manual/en/domdocument.getelementsbytagname.php
$objDOM = new DOMDocument('1.0');
$objDOM->preserveWhiteSpace = false;
$objDOM->loadXML($xml);