SimpleXML根据属性值获取元素内容
我试图根据属性的值访问元素的内容。使用 PHP SimpleXML。我有以下 XML 设置:
<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>
这些是书。在这种情况下,我正在努力获得标题。到目前为止我所得到的是:
$xml = simplexml_load_file(url);
$docs = $xml->DocSum;
foreach($docs as $book){
// Each book individual
}
在评论所在的地方我尝试了很多东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SimpleXML 对象上的此 XPath 查询将返回所有满足以下条件的
DocSum
节点:有一个Item
子项,其Name
属性中的值为“Author”,文本节点中的值为“Olivera GC”:This XPath query on the SimpleXML object will return all
DocSum
nodes that have anItem
child with value "Author" in theName
attribute and value "Olivera GC" in the text node:好吧,我怀疑解决方案将是
XPATH
...如果您尝试通过 id 获取一本书的标题,则可以使用以下查询:
如果您尝试搜索一本书的标题:
那么只需运行它:
I'm在 simplexml 和 domdocument 之间来回转换,因为这似乎是遍历树的唯一有记录的方式......
Well, I suspect the solution will be
XPATH
...If you're trying to get the title of a book by the id, you'd use the query:
If you're trying to search for the title of a book:
Then just run it:
I'm converting back and forth between simplexml and domdocument since that appears to be the only documented way of traversing up the tree...