2012-06-11 57 views
1

我想湯姆負載與多個命名空間聲明的XML文檔 我的PHP是:錯誤加載XML

<?php 
$doc = new DOMDocument('1.0','UTF-8'); 
$doc->load('UBLCatalog.xml'); 

$Items = $doc->getElementsByTagNameNS("UBLCommonAggregateComponents","Item"); 
foreach($Items as $Item) 
{ 
$descriptions = $Item->getElementsByTagNameNS("UBLCommonBasicComponents","Description"); 
$description = $descriptions->item(0)->nodeValue; 

echo "<b>$description\n</b><br>"; 
} 
?> 

的錯誤是:

xmlns: URI UBLCatalogDocument is not absolute in file:///C:/wamp/www/XMLExperiments/UBLCatalog.xml,

我得到輸出,但錯誤是煩人的。

逐字錯誤是:注意:DOMDocument :: load()[domdocument.load]:xmlns:URI UBLCatalogDocument在文件中不是絕對的:/// C:/wamp/www/XMLExperiments/UBLCatalog.xml,line :4在C:\ WAMP \ WWW \ XMLExperiments \ ItemsXml.php上線3

而且,如果我刪除默認的命名空間(的xmlns = 「UBLCatalogDocument」)錯誤消失

+1

您可以在這裏分享XML格式供他人查看。將有助於理解結構。 –

+0

檢查你的xml文件路徑。 – Dhruvisha

+0

什麼行導致此錯誤? –

回答

1

查找使用XPath的節點可能會擺脫您的錯誤:

$xpath = new DOMXpath($doc); 
$Items = $xpath->query('//item[@xmlns="UBLCommonAggregateComponents"]'); 
2

PHP XML擴展(DOM,XMLReader,SimpleXml等) use libxml library來解析XML文件。

此錯誤的原因是的libxml希望絕對URL在xmlns atttibute(即xmlns="http://example.com/some-unique-url"),但得到的純文本(xmlns="UBLCatalogDocument")。

libxml webpage

The namespace value has to be an absolute URL, but the URL doesn't have to point to any existing resource on the Web.

所以,如果有可能,你xmlns屬性更改爲某個絕對URL。