php DomDocument,仅需要第一项 [英] php DomDocument, require first item only

查看:47
本文介绍了php DomDocument,仅需要第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我过去曾经使用过DOMDocs,但是我仍然坚持如何执行此操作。

Hello I have use DOMDocs in the past but I am stuck how to do this.

$xmldoc = new DOMDocument();
$xmldoc->load('http://example.com');
$feeditem = $xmldoc->getElementsByTagName('data');

我只想要标记 data的第一个实例。通常,我会写一个foreach循环:

I only want the first instance of the tag 'data'. Ordinarily I would write a foreach loop:

foreach($feeditem as $element){ //}

并按照每个供稿项执行我想要的操作,但是在这里,我只希望第一个供稿项。 $ feeditem不是一个数组,它是一个DOMdoc对象,因此我无法执行$ feeditem [0]选择第一个数据标记。

and do what i want with each feeditem but here I only want the first $feeditem. $feeditem isn't an array, it's a DOMdoc object so I can't do $feeditem[0] to select the first 'data' tag.

谢谢

推荐答案

如果您查找 getElementsByTagName ,您会看到它返回了 DOMNodeList 。因此,在阅读 DOMNodeList 文档之后,您应该发现可以使用 $ feeditem-> item(0)

If you look up the documentation for getElementsByTagName, you can see that it returns a DOMNodeList. So after reading the documentation for DOMNodeList, you should find out that you can use $feeditem->item(0).

这篇关于php DomDocument,仅需要第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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