Parsing xml with dom4j or jdom or anyway

I want to read feed entries, and now I'm just stuck. Take this, for example: https://stackoverflow.com/feeds/question/2084883 lets say that I want to read all the node summary values ​​inside each node record in the document. How should I do it? I have changed many variations of the code that 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 through all the nodes in the XML file and writes their name. Now I wanted to do the following:

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

to get only input nodes, how do I get input node elements and how to get let say summary and its value? Tnx

Question : how to get the values ​​of the resulting nodes from this link

+3
4

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

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...
}
+2
if(elem.getName() == "entry")

, ( , ), --. equals():

if(elem.getName().equals("entry"))
+1

, , Google...

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

.

0

Source: https://habr.com/ru/post/1745291/


All Articles