3

我有以下需要从中解析值的 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 和带有 rel="thumb" 的图片 href 链接

通过检查here上的大量帖子和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'];

}

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

4

1 回答 1

0

为 XML 解析器尝试PHP DOMDocument。请参阅以下 URL 以获取标签和属性值。

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

$objDOM = new DOMDocument('1.0'); 
$objDOM->preserveWhiteSpace = false;
$objDOM->loadXML($xml);
于 2013-09-04T05:52:12.817 回答