views:

478

answers:

3

Hello, I wanna read feed entries and I'm just stuck now. Take this for example : http://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

+1  A: 

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...
}
Gala101
@Gala101 I'm using dom4j, its not an option sorry
c0mrade
+1  A: 
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"))
Anon
+1  A: 

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());
}
Michael Angstadt
@mangst this code works if you remove atom:summary part, it loops trough every entry and prints out values of child nodes. Question: How to do this. If there are any child nodes loop and get their value, if there are no more children or children values collect their attribute value else get child node attribute value
c0mrade
@c0mrade: See the edit to my code sample.
Michael Angstadt