在PHP中将父节点从XML文件获取为字符串


Getting Parent Node from XML file into a String in PHP

所以,我从XML提要解析数据到php变量,一切都很好,除了"link"元素。它不像其他孩子。

下面是一个更清晰、更简单的结构示例:
<bookstore>
  <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
    <link href="http://example.com">
  </book>
  <book category="web">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
    <link href="http://example.com">
  </book>
</bookstore>
<bookstore>
  <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
    <link href="http://example.com">
  </book>
  <book category="web">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
    <link href="http://example.com">
  </book>
</bookstore>

如何读取父节点/bookstore/中XML的link/href部分并将其放入字符串中?它的格式看起来很糟糕,但我无法更改它,因为它是由第三方提供的。

我认为我可以加载整个/bookstore/parent,然后在其中搜索链接并以这种方式拉出值,但它不会加载整个bookstore元素。

我的代码还提取了其他子标记,并通过循环运行以显示列表中的数据。如有任何帮助,不胜感激。

编辑:这是我必须使用的XML文件的链接:https://www.reddit.com/r/elderscrollsonline.xml

对于SimpleXML -此代码:

$rss = 'some_url_here';
$xml = simplexml_load_file($rss);

For you xml:

foreach($xml->bookstore as $bookstore) {
foreach ($bookastore as $book)
    echo (string)$book->link['href'];
}

用于https://www.reddit.com/r/elderscrollsonline.xml:

中的链接
foreach($xml->entry as $book) echo (string)$book->link['href'];