使用 dom4j 或 jdom 或其他方式解析 xml

发布于 2024-09-01 01:52:05 字数 715 浏览 7 评论 0原文

我想阅读提要条目,但现在被困住了。以此为例: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2024-09-08 01:52:05

你试过jdom吗?我觉得它更简单、更方便。

http://www.jdom.org/

要获取 xml 元素的所有子元素,您可以这样做

SAXBuilder sb = new SAXBuilder();
            StringReader sr = new StringReader(xmlDocAsString);
            Document doc = sb.build(sr);
            Element root = doc.getRootElement();
            List l = root.getChildren("entry");
            for (Iterator iter = l.iterator(); iter.hasNext();) {
...//do whatever...
}

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

SAXBuilder sb = new SAXBuilder();
            StringReader sr = new StringReader(xmlDocAsString);
            Document doc = sb.build(sr);
            Element root = doc.getRootElement();
            List l = root.getChildren("entry");
            for (Iterator iter = l.iterator(); iter.hasNext();) {
...//do whatever...
}
白鸥掠海 2024-09-08 01:52:05

以下是使用普通 Java 的方法:

//read the XML into a DOM
StreamSource source = new StreamSource(new StringReader("<theXml></theXml>"));
DOMResult result = new DOMResult();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
Node root = result.getNode();

//make XPath object aware of namespaces
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext(){
    @Override
    public String getNamespaceURI(String prefix) {
        if ("atom".equals(prefix)){
            return "http://www.w3.org/2005/Atom";
        }
        return null;
    }

    @Override
    public String getPrefix(String namespaceURI) {
        return null;
    }

    @Override
    public Iterator getPrefixes(String namespaceURI) {
        return null;
    }
});

//get all summaries
NodeList summaries = (NodeList) xpath.evaluate("/atom:feed/atom:entry/atom:summary", root, XPathConstants.NODESET);
for (int i = 0; i < summaries.getLength(); ++i) {
    Node summary = summaries.item(i);

    //print out all the attributes
    for (int j = 0; j < summary.getAttributes().getLength(); ++j) {
        Node attr = summary.getAttributes().item(j);
        System.out.println(attr.getNodeName() + "=" + attr.getNodeValue());
    }

    //print text content
    System.out.println(summaries.item(i).getTextContent());
}

Here's how you'd do it using vanilla Java:

//read the XML into a DOM
StreamSource source = new StreamSource(new StringReader("<theXml></theXml>"));
DOMResult result = new DOMResult();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
Node root = result.getNode();

//make XPath object aware of namespaces
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext(){
    @Override
    public String getNamespaceURI(String prefix) {
        if ("atom".equals(prefix)){
            return "http://www.w3.org/2005/Atom";
        }
        return null;
    }

    @Override
    public String getPrefix(String namespaceURI) {
        return null;
    }

    @Override
    public Iterator getPrefixes(String namespaceURI) {
        return null;
    }
});

//get all summaries
NodeList summaries = (NodeList) xpath.evaluate("/atom:feed/atom:entry/atom:summary", root, XPathConstants.NODESET);
for (int i = 0; i < summaries.getLength(); ++i) {
    Node summary = summaries.item(i);

    //print out all the attributes
    for (int j = 0; j < summary.getAttributes().getLength(); ++j) {
        Node attr = summary.getAttributes().item(j);
        System.out.println(attr.getNodeName() + "=" + attr.getNodeValue());
    }

    //print text content
    System.out.println(summaries.item(i).getTextContent());
}
轻许诺言 2024-09-08 01:52:05
if(elem.getName() == "entry")

我不知道这是否是您的问题(您并没有真正说明您的问题是什么),但是从不使用 -- 测试字符串相等性。相反,使用equals()

if(elem.getName().equals("entry"))
if(elem.getName() == "entry")

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, use equals():

if(elem.getName().equals("entry"))
浊酒尽余欢 2024-09-08 01:52:05

有点晚了,但它可能对人们谷歌搜索很有用...

Java 中有一个专门的 API 可以处理 RSS 和 Atom feeds。它被称为罗马,可以在这里找到:

http://java.net/projects/rome/

它确实非常有用,无论 RSS 或 Atom 版本如何,它都可以轻松阅读提要。您还可以构建提要并使用它生成 XML,尽管我对此功能没有经验。

这是一个简单的示例,它读取 feed 并打印出 feed 中所有条目的描述节点:

URL feedSource = new URL("http://....");
feed = new SyndFeedInput().build(new XmlReader(feedSource));
List<SyndEntryImpl> entries = (List<SyndEntryImpl>)feed.getEntries();

for(SyndEntryImpl entry : entries){
    System.out.println(entry.getDescription().getValue());
}

足够简单。

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 :

URL feedSource = new URL("http://....");
feed = new SyndFeedInput().build(new XmlReader(feedSource));
List<SyndEntryImpl> entries = (List<SyndEntryImpl>)feed.getEntries();

for(SyndEntryImpl entry : entries){
    System.out.println(entry.getDescription().getValue());
}

Simple enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文