【发布时间】:2011-08-31 22:58:58
【问题描述】:
我想用 HtmlUnit 解析 Feedburner 提要。 提要是这个:http://feeds.feedburner.com/alcoanewsreleases
从此提要中,我想读取所有 item 节点,所以通常//item XPath 应该可以解决问题。不幸的是,这在这种情况下不起作用。
groovy 代码 sn-p:
def page = webClient.getPage("http://feeds.feedburner.com/alcoanewsreleases")
def elements = page.getByXPath("//item")
XML 提要示例:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss1full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
[...SNIP...]
<item rdf:about="http://www.alcoa.com/global/en/news/news_detail.asp?newsYear=2011&pageID=20110518006002en">
<title>Chris L. Ayers Named President, Alcoa Global Primary Products</title>
<dc:date>2011-05-18</dc:date
<link>http://feedproxy.google.com/~r/alcoanewsreleases/~3/PawvdhpJrkc/news_detail.asp</link>
<description>NEW YORK--(BUSINESS WIRE)--Alcoa (NYSE:AA) announced today that Chris L. Ayers has been named President of Alcoa’s Global Primary Products (GPP) business, effective May 18, 2011. Ayers, previously Chief Operating Officer of GPP, succeeds John Thuestad, who will be handling special projects for the Company. Ayers joined Alcoa in February 2010 as Chief Operating Officer of Alcoa Cast, Forged and Extruded Products, a new position. He was elected a Vice President of Alcoa in April 2010 and Executive</description>
<feedburner:origLink xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://www.alcoa.com/global/en/news/news_detail.asp?newsYear=2010&pageID=20100104006194en</feedburner:origLink>
</item>
[...SNIP...]
</rdf:RDF>
我怀疑这是命名空间的问题,因为此文档有 4 个命名空间。命名空间是
- (这是默认设置) xmlns="http://purl.org/rss/1.0/"
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"
我已尝试将 Nokogiri 与此一起使用(我用于 ruby 脚本的另一个 XML 解析器)。
使用 Nokogiri,我可以只使用 XPath //xmlns:item,它可以工作并从提要中返回所有节点。
我用 HtmlUnit 尝试了相同的 XPath,但它不起作用。
所以我想我可以将我的问题表述为: 如何使用 HtmlUnit 从默认命名空间中选择节点?
有什么想法吗?
【问题讨论】:
标签: xpath groovy namespaces xml-namespaces htmlunit