gpt4 book ai didi

PHP SimpleXML 使用 xPath 读取注释

转载 作者:行者123 更新时间:2023-12-02 04:27:27 26 4
gpt4 key购买 nike

我正在加载 XML Feed,它可以工作。但我的 xpath 查询中似乎遗漏了一些东西。其实我想阅读标题节点的注释,但它似乎不起作用。

$xml = simplexml_load_file( $feed_url );
$comment = $xml->xpath('//channel/item[1]/title//comment()');

提要具有以下结构

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>My main feed</title>
<description>feed description</description>
<link>http://www.example.com</link>
<language>en_US</language>
<lastBuildDate>Wed, 26 Nov 2014 11:44:13 UTC</lastBuildDate>

<item>
<title><!-- Special Image --></title>
<link></link>
<guid>http://www.example.com/page/345</guid>
<media:category>horizontal</media:category>
<media:thumbnails>
<media:thumbnail url="www.example.com/test.jpg" type="image/jpeg" height="324" width="545" />
</media:thumbnails>
</item>
<item>
<title>Here's a normal title</title>
<description><![CDATA[Description Text]]></description>
<link></link>
<guid>http://www.example.com/page/123</guid>
<media:category>horizontal</media:category>
</item>
</channel>
</rss>

有人知道我如何阅读评论吗?

最佳答案

或者,您可以使用 DOMDocument 来访问这些注释。示例:

$dom = new DOMDocument();
$dom->load($feed_url);
$xpath = new DOMXpath($dom);
$comment = $xpath->evaluate('string(//channel/item[1]/title/comment())');
echo $comment;

关于PHP SimpleXML 使用 xPath 读取注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27147700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com