Karem
Karem

Reputation: 18103

DOMDocument simple GetElementsByTagName wont work?

        $xml = '<?xml version="1.0" encoding="UTF-8"?> 
<stw:ThumbnailResponse xmlns:stw="http://www.shrinktheweb.com/doc/stwresponse.xsd">
    <stw:Response>
        <stw:ThumbnailResult>
            <stw:Thumbnail Exists="true">http://imagelink.com</stw:Thumbnail>
            <stw:Thumbnail Verified="false">delivered</stw:Thumbnail>
        </stw:ThumbnailResult>
        <stw:ResponseStatus>
            <stw:StatusCode>refresh</stw:StatusCode>
        </stw:ResponseStatus>
        <stw:ResponseTimestamp>
            <stw:StatusCode>1413812009</stw:StatusCode>
        </stw:ResponseTimestamp>
        <stw:ResponseCode>
            <stw:StatusCode>HTTP:200</stw:StatusCode>
        </stw:ResponseCode>
        <stw:CategoryCode>
            <stw:StatusCode></stw:StatusCode>
        </stw:CategoryCode>
        <stw:Quota_Remaining>
            <stw:StatusCode>132</stw:StatusCode>
        </stw:Quota_Remaining>
        <stw:Bandwidth_Remaining>
            <stw:StatusCode>999791</stw:StatusCode>
        </stw:Bandwidth_Remaining>
    </stw:Response>
</stw:ThumbnailResponse>';

        $dom = new DOMDocument;
        $dom->loadXML($xml);


        $result = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
        $status = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;

        echo $result;

Having the above code should output http://imagelink.com and $status should hold "delivered" - but none of these work instead I am left with the error notice that:

Trying to get property of non-object

I have tried different xml parsing alternatives like simplexml (but that did not work when the tag names have : in it ) and i tried looping through the each scope in the xml (ThumbNailresponse, response and then thumbnailresult) without luck.

How can i get the values inside stw:Thumbnail?

Upvotes: 2

Views: 114

Answers (3)

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2115

You need to specify a namespace and the method DOMDocument::getElementsByTagName can't handle it. In the manual:

The local name (without namespace) of the tag to match on.

You can use DOMDocument::getElementsByTagNameNS instead:

$dom = new DOMDocument;
$dom->loadXML($xml);

$namespaceURI = 'http://www.shrinktheweb.com/doc/stwresponse.xsd';
$result = $dom->getElementsByTagNameNS($namespaceURI, 'Thumbnail')->item(0)->nodeValue;

Upvotes: 1

Kevin
Kevin

Reputation: 41885

Using simple xml you could use ->children() method on this one:

$xml = simplexml_load_string($xml_string);
$stw = $xml->children('stw', 'http://www.shrinktheweb.com/doc/stwresponse.xsd');
echo '<pre>';
foreach($stw as $e) {
    print_r($e);
    // do what you have to do here
}

Upvotes: 0

rm-vanda
rm-vanda

Reputation: 3158

This code actually runs just fine for me ---

Typically, that sort of error means you may've made a typo on your $dom object - double check it and try again.

Also, it is notable that you'll want to change the item(0) to item(1) when you're setting your $status variable.

    $result = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;
    $status = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;

Upvotes: 0

Related Questions