SimpleXML get element content based on attribute value(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
}
评论在哪里我尝试了很多东西.
Where the comment is I tried a lot of things.
推荐答案
这个 XPath 查询在 SimpleXML 对象上将返回所有 DocSum 节点,这些节点具有 Item 子节点,Name 属性中的值为Author",值为Olivera GC"在文本节点中:
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 根据属性值获取元素内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SimpleXML 根据属性值获取元素内容
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
