使用 JDOM 执行 getChild() 时返回空引用
我有 XML 文档的下一个示例,我需要从文档中分离所有“链接”元素
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
<channel>
<title>Slashdot</title>
<link>http://slashdot.org/</link>
<description>News for nerds, stuff that matters</description>
</channel>
<image>
<title>Slashdot</title>
<url>http://a.fsdn.com/sd/topics/topicslashdot.gif</url>
<link>http://slashdot.org/</link>
</image>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/xml" href="http://rss.slashdot.org/Slashdot/slashdot/to" />
</rdf:RDF>
这是我正在使用的代码
while (iterator.hasNext()) {
Object next = iterator.next();
Element element = (Element) next;
Namespace namespace = element.getNamespace();
Element link = element.getChild("link",namespace);
link.detach();
}
它在没有属性的“链接”元素上运行良好
<link>http://slashdot.org/</link>
<link>http://slashdot.org/</link>
但是当我想获取下一个也是链接的子元素时,但对于属性和不同的命名空间,返回空对象引用
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/xml" href="http://rss.slashdot.org/Slashdot/slashdot/to" />
请帮助,
非常感谢 大卫
I have next sample of XML document and I need to detach all "link" Elements from document
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
<channel>
<title>Slashdot</title>
<link>http://slashdot.org/</link>
<description>News for nerds, stuff that matters</description>
</channel>
<image>
<title>Slashdot</title>
<url>http://a.fsdn.com/sd/topics/topicslashdot.gif</url>
<link>http://slashdot.org/</link>
</image>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/xml" href="http://rss.slashdot.org/Slashdot/slashdot/to" />
</rdf:RDF>
That's a code I am using
while (iterator.hasNext()) {
Object next = iterator.next();
Element element = (Element) next;
Namespace namespace = element.getNamespace();
Element link = element.getChild("link",namespace);
link.detach();
}
It's work well on "link" Elements without attributes
<link>http://slashdot.org/</link>
<link>http://slashdot.org/</link>
But when I want to get a next child which is also a link , but with attributes and different namespace, null object reference is returned
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/xml" href="http://rss.slashdot.org/Slashdot/slashdot/to" />
Please help
Thanks a lot
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要删除所有具有本地名称“link”的元素,无论命名空间如何,最简单的解决方案是 XPath。表达式
//*[local-name() = 'link]
将选择文档中本地名称为 'link' 的所有元素节点。If you want to remove all elements with local name 'link', regardless of the namespace, the easiest solution is XPath. The expression
//*[local-name() = 'link]
will select all element nodes in the document whose local name is 'link'.