Domanda

I Wanna leggere le voci di alimentazione e sto appena bloccato ora. Prendete questo esempio: https://stackoverflow.com/feeds/question/2084883 Diciamo che voglio leggere tutto il riepilogo valore del nodo all'interno di ciascun nodo entrata nel documento. Come lo faccio? Ho cambiato molte varianti di codice di questo è più vicino a quello che voglio raggiungere penso:

Element entryPoint = document.getRootElement();
  Element elem;
  for(Iterator iter = entryPoint.elements().iterator(); iter.hasNext();){
   elem = (Element)iter.next();
                    System.out.println(elem.getName());
  }

Si va attraverso tutti i nodi in un file XML e scrive il loro nome. Ora quello che volevo fare dopo è

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

per ottenere solo i nodi di ingresso, come faccio a ottenere elementi dei nodi di ingresso, e come ottenere lasciare sintesi diciamo e il suo valore? TNX

Domanda : come ottenere i valori dei nodi di riepilogo da questo link

È stato utile?

Soluzione

Ecco come si farebbe usando vaniglia 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());
}

Altri suggerimenti

Hai provato JDOM? Trovo più semplice e comodo.

http://www.jdom.org/

Per ottenere tutti i bambini di un elemento XML, si può solo fare

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

Non ho idea se questo è il vostro problema (non si ha realmente stato che cosa il vostro problema è), ma non l'uguaglianza stringa di prova con --. Invece, l'uso equals():

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

Un po 'in ritardo, ma potrebbe essere utile per le persone che googling ...

C'è un'API specializzata per trattare con feed RSS e Atom in Java. Si chiama Roma, può essere trovato qui:

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

E 'davvero molto utile, rende facile da leggere Feed RSS qualunque sia la versione o Atom. È inoltre possibile creare i feed e generare il codice XML con esso se non ho esperienza con questa funzione.

Ecco un semplice esempio che legge un feed e stampe out la descrizione nodi di tutte le voci del 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());
}

abbastanza semplice.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top