如何使用简单 xml 解析 xml - 带空格和非标准格式的节点

How to parse xml with simplexml - nodes with spaces & non standard format

下面显示了一个简单的 xml 节点,我可以通过原子提要循环并获取标题,这一切都非常简单。

<title>Soft Golden Yellow Champagne Wedding Fizzy Bubbles </title>

我的问题是像这样的节点没有像简单的那样格式化:

<link rel="enclosure" type="image/jpeg" href="https://someimage.com/image.jpg"/>

你如何存储和回显它?我获取以下标题的代码

$html = "";
$url = "https://www.redbubble.com/people/honorandobey/shop/recent+drawstring-bags.atom";
$xml = simplexml_load_file($url);
for($i =0; $i < 10; $i++) {
    $title =$xml->entry[$i]->title;
    $html .="<p>$title</p>";
}
echo $html;

如何获取 images/links 或任何格式类似于 link 的 rel 节点?

使用 SimpleXMLElement 您可以将节点的属性作为数组访问,因此要获取例如 link 标签的 href 属性,请执行以下操作:

$xml = simplexml_load_file($url);
for($i =0; $i < 10; $i++) {
    $title =$xml->entry[$i]->title;
    $html .= "<p>$title</p>";
    $html .= "<p>Link: ".$xml->entry[$i]->link["href"]."</p>";
}
echo $html;

或者您也可以使用 attributes() 方法访问属性:

$html .= "<p>Link: ".$xml->entry[$i]->link->attributes()->href."</p>";