PHP DOMDocument获取标签的属性 [英] PHP DOMDocument getting Attribute of Tag

查看:157
本文介绍了PHP DOMDocument获取标签的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个api响应的xml格式与一系列的项目,如:

 < item> 
< title> blah balh< / title>
< pubDate> Tue,20 Oct 2009< / pubDate>
< media:file date =todaydata =example text string/>
< / item>

我想使用DOMDocument从标签media:file获取属性data。我下面的尝试不起作用:

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

我做错了什么?请帮助。



编辑:我不能因某种原因留下评论。我收到错误

 调用非对象的成员函数getAttribute()

我也试过

  $ nodes = $ feeditem-> getElementsByTagNameNS('uri','file'); 
$ linkthumb = $ nodes-> item(0) - > getAttribute('data');

其中uri是与媒体名称空间(NS)有关的uri,但是同样的问题。 / p>

请注意,媒体元素的形式不是我认为这是问题的一部分,因为我通常没有解析attibutes的问题。

解决方案

您提供的示例不应该生成错误。我测试了它,$ linkthumb包含字符串示例文本字符串如预期



确保媒体命名空间在返回的XML中定义,否则DOMDocument将出错。



如果您收到特定错误,请编辑您的帖子以包含



编辑:



尝试以下代码:

  $ 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;
}

您可能还想查看 SimpleXML Xpath ,因为它使阅读XML比DOMDocument更容易。


Hello I have an api response in xml format with a series of items such as this:

<item>
<title>blah balh</title>
<pubDate>Tue, 20 Oct 2009 </pubDate>
<media:file  date="today" data="example text string"/>
</item>

I want to use DOMDocument to get the attribute "data" from the tag "media:file". My attempt below doesn't work:

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

What am I doing wrong? Please help.

EDIT: I can't leave comments for some reason Mark. I get the error

 Call to a member function getAttribute() on a non-object

when I run my code. I have also tried

$nodes = $feeditem->getElementsByTagNameNS('uri','file');
$linkthumb = $nodes->item(0)->getAttribute('data');

where uri is the uri relating to the media name space(NS) but again the same problem.

Note that the media element is of the form not I think this is part of the problem, as I generally have no issue parsing for attibutes.

解决方案

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.

这篇关于PHP DOMDocument获取标签的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆