2016-01-21 138 views
0

我無法從這個XML得到以下信息命名空間:解析XML APFeeds使用PHP和SimpleXML

<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" /> 

具體來說,我需要的「ID」和「價值」字段。

這裏是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職位,以揣摩出來,而這一次是最有幫助的,到目前爲止:Identical nested XML elements with namespaces and PHP

而這裏的是我與打碼:

$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

我想你可以註冊名稱空間,然後使用這個xpath表達式得到的元素:

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

$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是:金塔:publicid:ap.org:產品:41664和值爲:AP前 新聞

ID是:甕:publicid:ap.org:產品:42430和值爲:AP前 新聞 - 國際 - 故事

ID是:金塔:publicid:ap.org:包:100518和值是:AP 頂部新聞套餐

Demo