dtf76989 2016-01-21 21:36
浏览 47
已采纳

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

I'm having trouble getting the following information from this 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" />

Specifically, I need the "ID" and "Value" fields.

Here's the main parts of the 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>

I have been looking at the following SO posts to try to figure it out, and this one is the most helpful so far: Identical nested XML elements with namespaces and PHP

And here's the code that I'm playing with:

    $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
", $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条回答 默认 最新

  • dp152153 2016-01-22 09:42
    关注

    I think you can register the namespace and then use this xpath expression to get the elements:

    $elements = $feed_entry->xpath('//a:entry/apcm:ContentMetadata/apcm:Property');

    The $elements is an array of SimpleXMLElement objects from which you can get the 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>";
    }
    

    Will result in:

    The Id is: urn:publicid:ap.org:product:41664 and the Value is: AP Top News

    The Id is: urn:publicid:ap.org:product:42430 and the Value is: AP Top News - International - Stories

    The Id is: urn:publicid:ap.org:package:100518 and the Value is: AP Top News Package

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料