DOMDocument 简单的 GetElementsByTagName 不起作用? [英] DOMDocument simple GetElementsByTagName wont work?
问题描述
$xml = 'loadXML($xml);$result = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;$status = $dom->getElementsByTagName('stw:Thumbnail')->item(0)->nodeValue;回声 $result;
上面的代码应该输出 http://imagelink.com 并且 $status 应该保持delivered"——但没有在这些工作中,我留下了错误通知:
试图获取非对象的属性
我尝试了不同的 xml 解析替代方案,例如 simplexml(但是当标签名称中包含 : 时这不起作用)并且我尝试循环遍历 xml 中的每个范围(ThumbNailresponse、response 然后thumbnailresult),但没有走运.
如何获取 stw:Thumbnail 中的值?
使用简单的 xml 你可以使用 ->children()
方法:
$xml = simplexml_load_string($xml_string);$stw = $xml->children('stw', 'http://www.shrinktheweb.com/doc/stwresponse.xsd');echo '';foreach($stw as $e) {打印_r($e);//在这里做你必须做的事}
$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?
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
}
这篇关于DOMDocument 简单的 GetElementsByTagName 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!