2009-08-18 15 views
12
try { 
     String data = "<a><b c='d' e='f'>0.15</b></a>"; 
     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory 
       .newInstance(); 
     DocumentBuilder documentBuilder = documentBuilderFactory 
       .newDocumentBuilder(); 
     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(data)); 
     Document document = documentBuilder.parse(is); 

     NodeList nl = document.getElementsByTagName("b"); 
     Node n = (Node) nl.item(0); 
     System.out.println(n.getNodeValue()); 
    } catch (Exception e) { 
     System.out.println("Exception " + e); 

    } 

Mi aspetto che stampi 0.15, ma stampa nulla. Qualche idea?Analisi XML Java utilizzando DOM per ottenere nodevalue

Edit: questo ha fatto il trucco

 if (n.hasChildNodes()) 
      System.out.println(n.getFirstChild().getNodeValue()); 
     else 
      System.out.println(n.getNodeValue()); 

risposta

6

Prova estraendolo dal Element, piuttosto che dal Nodo:

try { 
    String data = "<a><b c='d' e='f'>0.15</b></a>"; 
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory 
      .newInstance(); 
    DocumentBuilder documentBuilder = documentBuilderFactory 
      .newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(data)); 
    Document document = documentBuilder.parse(is); 

    NodeList nl = document.getElementsByTagName("b"); 
    Element el = (Element) nl.item(0); 
    Text elText = (Text) el.getFirstChild(); 
    String theValue = elText.getNodeValue(); 
    System.out.println(theValue); 
} catch (Exception e) { 
    System.out.println("Exception " + e); 
} 
+0

@mkal - ho solo modificato per l'uscita del nodeValue invece dell'istanza di testo! – karim79

11

Questo perché an element in fact has no nodeValue. Invece ha un nodo di testo come un bambino, che ha il nodeValue desiderato.

In breve, ti consigliamo di getNodeValue() sul primo figlio del nodo elemento.

A volte l'elemento contiene più nodi di testo, come essi hanno una dimensione massima, in questo caso avrete bisogno di qualcosa à la presente, dalla pagina collegata in precedenza:

public static String getNodeValue(Node node) { 
    StringBuffer buf = new StringBuffer(); 
    NodeList children = node.getChildNodes(); 
    for (int i = 0; i < children.getLength(); i++) { 
     Node textChild = children.item(i); 
     if (textChild.getNodeType() != Node.TEXT_NODE) { 
      System.err.println("Mixed content! Skipping child element " + textChild.getNodeName()); 
      continue; 
     } 
     buf.append(textChild.getNodeValue()); 
    } 
    return buf.toString(); 
} 
+0

Buona spiegazione e grazie per l'eccellente funzione. Funziona come un campione. –

3
System.out.println(n.getFirstChild().getNodeValue()); 
1

Se il il nodo non ha altri discendenti nidificati, rispetto a n.getTextContent() funziona abbastanza bene.

2
private String getTextValue(Element element, String string) { 
    String textVal = null; 
    NodeList nl = element.getElementsByTagName(string); 
    if(nl != null && nl.getLength() > 0) { 
     Element el = (Element)nl.item(0); 
     textVal = el.getFirstChild().getNodeValue(); 
    } 

    return textVal; 

} 
1

Si potrebbe utilizzare jOOX come wrapper per standard DOM, per semplificare il codice.

String data = "<a><b c='d' e='f'>0.15</b></a>"; 
String value = $(data).find("b").text(); 

Si potrebbe anche avere Joox convertire quel valore di raddoppiare, per esempio:

Double value = $(data).find("b").text(Double.class);