PHP DOMDocument getting Attribute of Tag

回眸只為那壹抹淺笑 提交于 2019-11-27 14:49:37

The example you provided should not generate an error. I tested it and $linkthumb contained the string "example text string" as expected

Ensure the media namespace is defined in the returned XML otherwise DOMDocument will error out.

If you are getting a specific error, please edit your post to include it

Edit:

Try the following code:

$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
    $nodes = $feeditem->getElementsByTagName('file');
    $linkthumb = $nodes->item(0)->getAttribute('data');
    echo $linkthumb;
}

You may also want to look at SimpleXML and Xpath as it makes reading XML much easier than DOMDocument.

Alternatively,

$DOMNode -> attributes -> getNamedItem( 'MyAttribute' ) -> value;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!