开发者

Problems accessing elements with namespaces in PHP using DOMDocument

Here is my XML fragment:

<?xml version="1.0" encoding="UTF-8"?>
<ns:searchResult xmlns:ns="http://outerx.org/daisy/1.0">
...
    <ns:rows>
        <ns:row documentId="1440-DFO_MPO" branchId="1" languageId="2"
            access="read,fullRead,write,publish">
            <ns:value>1440-DFO_MPO</ns:value>
            <ns:value>Navigation for Multimedia</ns:value>
        </ns:row>
    </ns:rows>
...

Here is my current PHP Code:

$dom = new DOMDocument();
$dom->load($xml);
$docs = $dom->getElementsByTagNameNS('http://outerx.org/daisy/1.0','row');

print "<ul>";
$c = 0;    
foreach ($docs as $elem) {
    print "<li>".$c."</li>";
    $c = $c + 1;
}    
print "</ul>";

AFAIK, this snippet should output a list of one item based on the XML fragment. However, it doesn't.

I've also tried (without success):

$docs = $dom->getElementsByTagName('开发者_如何转开发row');

Edit #1 - Solution

Changed $dom->load($xml) to $dom->loadXML($xml);


Your code works for me. I assume you are passing the XML content in $xml when load() expects it to be a filename or URI. To load XML content directly you have to use

  • DOMDocument::loadXML — Load XML from a string

See http://codepad.org/oy3U1fmE

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜