我有以下XML需要解析来自。
的值<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>
我需要从这个xml中提取以下数据
标题,描述 - 简单易于理解。做完了
我还需要public_link href和图片href链接,用于那些有rel =“thumb”
的人通过检查这里的大量帖子和php文档,我得出了类似的东西。
$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'];
}
出于某种原因,它不想拉动属性。有人能指出我正确的方向吗?
答案 0 :(得分:0)
为XML Parser尝试 PHP DOMDocument 。请参阅以下URL以获取标记和属性值。
http://www.php.net/manual/en/domdocument.getelementsbytagname.php
$objDOM = new DOMDocument('1.0');
$objDOM->preserveWhiteSpace = false;
$objDOM->loadXML($xml);