SimpleXML根据属性值获取元素内容

发布于 2024-10-13 03:29:19 字数 1055 浏览 3 评论 0原文

我试图根据属性的值访问元素的内容。使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-10-20 03:29:19

SimpleXML 对象上的此 XPath 查询将返回所有满足以下条件的 DocSum 节点:有一个 Item 子项,其 Name 属性中的值为“Author”,文本节点中的值为“Olivera GC”:

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

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);
因为看清所以看轻 2024-10-20 03:29:19

好吧,我怀疑解决方案将是 XPATH...

如果您尝试通过 id 获取一本书的标题,则可以使用以下查询:

$query = '//DocSum/Id[contains(., "21232919")]';

如果您尝试搜索一本书的标题:

$query = '//DocSum/Item[@Name="Title" AND contains(., "Metformin")]';

那么只需运行它:

$xml = simplexml_load_file($url);

$results = $xml->xpath($query);
foreach ($results as $titleElement) {
    $book = simplexml_import_dom(dom_import_simplexml($titleElement)->parentNode);
    // Handle the book here
}

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:

$query = '//DocSum/Id[contains(., "21232919")]';

If you're trying to search for the title of a book:

$query = '//DocSum/Item[@Name="Title" AND contains(., "Metformin")]';

Then just run it:

$xml = simplexml_load_file($url);

$results = $xml->xpath($query);
foreach ($results as $titleElement) {
    $book = simplexml_import_dom(dom_import_simplexml($titleElement)->parentNode);
    // Handle the book here
}

I'm converting back and forth between simplexml and domdocument since that appears to be the only documented way of traversing up the tree...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文