2011-03-29 64 views
0

我試圖解析XML文件中的PHP看起來像:解析XML與PHP包含名稱空間

<post dsq:id="##Edited out##"> 
    <id /> 
    <message>##Edited out##</message> 
    <createdAt>2010-07-09T16:08:44Z</createdAt> 
    <author> 
     <email>##Edited out##</email> 
     <name>##Edited out##</name> 
     <isAnonymous /> 
    </author> 
    <ipAddress>##Edited out##</ipAddress> 
    <thread dsq:id="##Edited out##" /> 
</post> 

<post dsq:id="##Edited out##"> 
    <id /> 
    <message>##Edited out##</message> 
    <createdAt>2010-07-09T16:10:07Z</createdAt> 
    <author> 
     <email>##Edited out##</email> 
     <name>##Edited out##</name> 
     <isAnonymous /> 
    </author> 
    <ipAddress>##Edited out##</ipAddress> 
    <thread dsq:id="##Edited out##" /> 
</post> 

我使用簡單的XML解析它是這樣的:

$xml = simplexml_load_file('disqus.xml'); 

foreach ($xml->post as $post) 
{ 
    echo $post['dsq:id']; 
} 

然而,我無法從後對象這裏的id屬性:

<post dsq:id="##Edited out##"> 

任何信息怎麼會被檢索會不勝感激。

+0

命名空間應該位於文檔根目錄下。它是什麼? – Gordon 2011-03-29 21:50:53

+0

<?xml version =「1.0」encoding =「utf-8」?> 2011-03-29 21:52:58

+0

dsq?也許可以嗎? – Treffynnon 2011-03-29 21:53:06

回答

1
$xml = simplexml_load_file('disqus.xml'); 

foreach ($xml->post as $post) 
{ 
    $dsq = $post->children('disqus.com/disqus-internals'); //should this not have http://disqus.com/disqus-internals? 
    echo $dsq->id; 
} 

您正在使用的那個w3.org/2001/XMLSchema-instance用於xsi命名空間。