SimpleXML基于属性值获取元素内容 [英] SimpleXML get element content based on attribute value

查看:101
本文介绍了SimpleXML基于属性值获取元素内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据属性的值访问元素的内容。使用PHP SimpleXML。我有以下XML设置:

I'm trying to access the content of an element based on the value of an attribute. With PHP SimpleXML. I've got the following XML setup:

<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Nguyen T</Item>
    <Item Name="Title" Type="String">[Hemoptysis and spontaneous rupture of a primary renal angiosarcoma: a case report.]</Item>
</DocSum>
<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Oliveira GC</Item>
    <Item Name="Title" Type="String">Disclosing ambiguous gene aliases by automatic literature profiling.</Item>
</DocSum>
<DocSum> 
    <Id>21242919</Id> 
    <Item Name="Author" Type="String">Vanderwall DE</Item>
    <Item Name="Title" Type="String">Metformin and digestive disorders.</Item>
</DocSum>

这些是书籍。在这种情况下,我想得到的标题。我到目前为止是这样:

These are books. In this case I'm trying to get the title. What I have so far is this:

$xml = simplexml_load_file(url);
$docs = $xml->DocSum;
foreach($docs as $book){
        // Each book individual
}


推荐答案

XPath 查询SimpleXML对象将返回所有 DocSum 节点在文本节点中的 Name 属性和值Olivera GC中具有值Author的 p>

This XPath query on the SimpleXML object will return all DocSum nodes that have an Item child with value "Author" in the Name attribute and value "Olivera GC" in the text node:

$nodes = $xml->xpath('//DocSum[Item[@Name="Author" and .="Oliveira GC"]]');
$book = $nodes[0];
print_r($book);

这篇关于SimpleXML基于属性值获取元素内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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