使用 dom4j 或 jdom 或其他方式解析 xml
我想阅读提要条目,但现在被困住了。以此为例: https://stackoverflow.com/feeds/question/2084883 假设我想阅读所有摘要文档中每个条目节点内的节点值。我该怎么做?我已经更改了代码的许多变体,这一个最接近我想要实现的目标:
Element entryPoint = document.getRootElement();
Element elem;
for(Iterator iter = entryPoint.elements().iterator(); iter.hasNext();){
elem = (Element)iter.next();
System.out.println(elem.getName());
}
它遍历 xml 文件中的所有节点并写入它们的名称。现在我接下来想做的是
if(elem.getName().equals("entry"))
仅获取入口节点,如何获取入口节点的元素,以及如何获取摘要及其值? tnx
问题:如何从此链接获取汇总节点的值
I wanna read feed entries and I'm just stuck now. Take this for example : https://stackoverflow.com/feeds/question/2084883 lets say I wanna read all the summary node value inside each entry node in document. How do I do that? I've changed many variations of code this one is closest to what I want to achieve I think :
Element entryPoint = document.getRootElement();
Element elem;
for(Iterator iter = entryPoint.elements().iterator(); iter.hasNext();){
elem = (Element)iter.next();
System.out.println(elem.getName());
}
It goes trough all nodes in xml file and writes their name. Now what I wanted to do next is
if(elem.getName().equals("entry"))
to get only the entry nodes, how do I get elements of the entry nodes, and how to get let say summary and its value? tnx
Question: how to get values of summary nodes from this link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你试过jdom吗?我觉得它更简单、更方便。
http://www.jdom.org/
要获取 xml 元素的所有子元素,您可以这样做
Have you tried jdom? I find it simpler and convenient.
http://www.jdom.org/
To get all children of an xml element, you can just do
以下是使用普通 Java 的方法:
Here's how you'd do it using vanilla Java:
我不知道这是否是您的问题(您并没有真正说明您的问题是什么),但是从不使用
--
测试字符串相等性。相反,使用equals()
:I have no idea whether this is your problem (you don't really state what your problem is), but never test string equality with
--
. Instead, useequals()
:有点晚了,但它可能对人们谷歌搜索很有用...
Java 中有一个专门的 API 可以处理 RSS 和 Atom feeds。它被称为罗马,可以在这里找到:
http://java.net/projects/rome/
它确实非常有用,无论 RSS 或 Atom 版本如何,它都可以轻松阅读提要。您还可以构建提要并使用它生成 XML,尽管我对此功能没有经验。
这是一个简单的示例,它读取 feed 并打印出 feed 中所有条目的描述节点:
足够简单。
A bit late but it might be useful for people googling...
There is a specialized API for dealing with RSS and Atom feeds in Java. It's called Rome, can be found here :
http://java.net/projects/rome/
It is really quite useful, it makes easy to read feed whatever the RSS or Atom version. You can also build feeds and generate the XML with it though I have no experience with this feature.
Here is a simple example that reads a feed and prints out the description nodes of all the entries in the feed :
Simple enough.