I have got XML file. This is his structure:
我有XML文件。这是他的结构:
<data>
<pos1>
</pos1>
<pos2>
</pos2>
.
.
.
<pos3>
<pos1>
</pos1>
</pos3>
</data>
I want only first data from <pos1>
. If I use $xml->getElementsByTagName('pos1')
I received all data.
我只想要来自
Is it better way than get all elements from data and then search pos1?
它是比从数据中获取所有元素然后搜索pos1更好的方法吗?
1 个解决方案
#1
0
You can try using DOMXPath::query()
:
您可以尝试使用DOMXPath :: query():
$xpath = new DOMXPath($yourDomDocument);
$nodes = $xpath->query('//data/pos1');
Above xpath query only return <pos1>
that is child of <data>
.
上面的xpath查询只返回
的子节点
#1
0
You can try using DOMXPath::query()
:
您可以尝试使用DOMXPath :: query():
$xpath = new DOMXPath($yourDomDocument);
$nodes = $xpath->query('//data/pos1');
Above xpath query only return <pos1>
that is child of <data>
.
上面的xpath查询只返回
的子节点