TechFanDan
TechFanDan

Reputation: 3478

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);

Upvotes: 2

Views: 491

Answers (1)

Gordon
Gordon

Reputation: 316989

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

See http://codepad.org/oy3U1fmE

Upvotes: 2

Related Questions