Php XMLA解析以获得多个属性

Php XMLA解析以获得多个属性,php,xml,xml-parsing,Php,Xml,Xml Parsing,我有以下XML,需要从中解析值 <ads> <ad id="987654321"> <price> <currency-iso-code> <value localized-label="£">GBP</value> </currency-iso-code> <amount>

我有以下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>

英镑
345
周报
这是标题
描述
活跃的
存在
123456
123456
2013-09-02T11:40:41.000+01:00
2013-09-02T11:40:39.000+01:00
类别名称
类别名称
英国
大不列颠联合王国
邻里名
贸易
平的
1.
2013-05-15T00:00:00.000+01:00
我需要从这个xml中提取以下数据

标题,描述-易于拉取。完成了

我还需要public_link href和带有rel=“thumb”的图片href链接

通过查看这里的大量帖子和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'];

}
$ads=simplexml\u load\u字符串($xml);
foreach($ads作为$ads){
$item['price']=$ad->price->amount;
$item['title']=$ad->title;
$link=simplexml\u load\u string($ad->features\u active->public\u link);
foreach($link->attributes()作为$attr=>$value){
如果($attr=='href'){
$item['link']=$value;
}
}
$pics=simplexml\u load\u string($ad->features\u active->public\u link->pictures);
foreach($pics->picture[0]->attributes()作为$attr=>$value){
如果($attr=='thumb'){
$item['picture']=$value;
}
}
回显“

”$item['price'”; 回显“
”$item['title']; 回显“
”$item['link']; 回声“
”$item['picture']; }

出于某种原因,它不想提取属性。有人能为我指出正确的方向吗?

试试PHP DOMDocumentfor XML解析器。请参阅下面的URL以获取标记和属性值


您犯了一个错误:没有理由对同一文档多次调用
simplexml\u load\u string
。只要在上面一次就足够了。访问属性也很容易,请查看PHP手册(您在这里做的工作太多:))
$objDOM = new DOMDocument('1.0'); 
$objDOM->preserveWhiteSpace = false;
$objDOM->loadXML($xml);