XML 中缺少由 PHP 中的简单 XML 解析的 children

Missing children in XML parsed by SimpleXML in PHP

这是我正在解析的 XML: https://seekingalpha.com/api/sa/combined/AAPL.xml

当我用 simplexml_load_file($url) 抓取并解析 XML,然后对其执行 var_dump 时,它显示唯一的 child每 "item" 的人是 "title"、"link"、"guid" 和 "pubDate."

我正在尝试访问节点 "sa:author_name." 为什么它不是 "item" 的 child?也许我误解了 XML 文件的结构。帮帮我 children 不见了 lol

要获取 sa:author_name 中的数据,您必须使用命名空间 https://seekingalpha.com/api/1.0

例如,您可以使用 foreach 并使用命名空间循环 children

$url = "https://seekingalpha.com/api/sa/combined/AAPL.xml";
$xml = simplexml_load_file($url);

foreach ($xml->channel->item as $item) {
    foreach ($item->children("https://seekingalpha.com/api/1.0") as $child) {
        if ($child->getName() === "author_name") {
            echo $child . "<br>";
        }
    }
}

另一种方法是使用 xpath 表达式:

$authorNames = $xml->xpath('/rss/channel/item/sa:author_name');

foreach ($authorNames as $authorName) {
    echo $authorName . "<br>";
}

这将导致:

Yoel Minkoff
DoctoRx
SA Transcripts
Bill Maurer
etc..