3

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

4

4 回答 4

2

你试过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...
}
于 2010-05-13T15:03:05.223 回答
1

以下是使用 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());
}
于 2010-05-14T17:44:22.483 回答
1
if(elem.getName() == "entry")

我不知道这是否是你的问题(你并没有真正说明你的问题是什么),但永远不要--. 相反,使用equals()

if(elem.getName().equals("entry"))
于 2010-05-13T15:32:05.570 回答
0

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

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

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

它真的非常有用,无论是 RSS 还是 Atom 版本,它都可以轻松阅读提要。尽管我没有使用此功能的经验,但您也可以使用它构建提要并生成 XML。

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

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());
}

很简单。

于 2011-07-13T22:29:05.200 回答