使用PHP和SimpleXML解析APFeeds XML和名称空间

时间:2016-01-21 21:36:37

标签: php xml simplexml

我无法从此XML获取以下信息:

<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:41664" Value="AP Top News" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:42430" Value="AP Top News - International - Stories" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:package:100518" Value="AP Top News Package" />

具体来说,我需要&#34; ID&#34;和&#34;价值&#34;字段。

这是XML的主要部分:

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:georss="http://www.georss.org/georss" xmlns:o="http://w3.org/ns/odrl/2/">
...
<entry xmlns="http://www.w3.org/2005/Atom">
...
<apcm:ContentMetadata xmlns:apcm="http://ap.org/schemas/03/2005/apcm">
...
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:41664" Value="AP Top News" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:42430" Value="AP Top News - International - Stories" />
<apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:package:100518" Value="AP Top News Package" />
...
</apcm:ContentMetadata>
</entry>
</feed>

我一直在查看以下SO帖子以试图解决这个问题,到目前为止这个帖子是最有用的:here

以下是我正在玩的代码:

    $ns_dc = $feed_entry->children($ns['apcm']);
    echo "APCM children: " . count($ns_dc) . "<br />";

    $inner_ns_dc = $feed_entry->children($ns_dc["apcm:Property"]);
    echo "APCM Property Children: " . count($inner_ns_dc) . "<br />";

    //$sxe = new SimpleXMLElement($feed_entry);

    $sxe = new SimpleXMLElement($feed_entry->asXML());

    foreach($sxe->apcm as $item) {
        printf("%s\n", $item);
    }
    $sxe->registerXPathNamespace('apcm', 'http://ap.org/schemas/03/2005/apcm');
    $result = $sxe->xpath('/apcm:Property:*');

    echo "Result count: " . count($result) . "<br />";

    foreach ($result as $sequenceNumber) {
      echo $sequenceNumber . "<br />";
    }

1 个答案:

答案 0 :(得分:1)

我认为您可以注册命名空间,然后使用此xpath表达式来获取元素:

  

$ elements = $ feed_entry-&gt; xpath(&#39; // a:entry / apcm:ContentMetadata / apcm:Property&#39;);

$elementsSimpleXMLElement个对象的数组,您可以从中获取attributes

$source = <<<SOURCE
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org/schemas/03/2005/apcm"
      xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:georss="http://www.georss.org/georss"
      xmlns:o="http://w3.org/ns/odrl/2/">
    <entry xmlns="http://www.w3.org/2005/Atom">
        <apcm:ContentMetadata xmlns:apcm="http://ap.org/schemas/03/2005/apcm">
            <apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:41664" Value="AP Top News"/>
            <apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:product:42430" Value="AP Top News - International - Stories"/>
            <apcm:Property Name="EntitlementMatch" Id="urn:publicid:ap.org:package:100518" Value="AP Top News Package"/>
        </apcm:ContentMetadata>
    </entry>
</feed>
SOURCE;

$feed_entry = simplexml_load_string($source);
$feed_entry->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$elements = $feed_entry->xpath('//a:entry/apcm:ContentMetadata/apcm:Property');

foreach ($elements as $element) {
    $id = $element->attributes()->Id->__toString();
    $value = $element->attributes()->Value->__toString();

    echo "The Id is: $id and the Value is: $value<br>";
}

将导致:

  

Id为:urn:publicid:ap.org:product:41664,Value为:AP Top   消息

     

ID为:urn:publicid:ap.org:product:42430,Value为:AP Top   新闻 - 国际 - 故事

     

Id为:urn:publicid:ap.org:package:100518,Value为:AP   热门新闻包

Demo

相关问题