2009-08-18 27 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); 

    } 

Spodziewam się, że wydrukuje 0.15, ale wydrukuje zero. Jakieś pomysły?Parsowanie XML XML za pomocą DOM, aby uzyskać wartość węzła,

Edit: To załatwiło sprawę

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

Odpowiedz

6

spróbować wydobywania go z elementu zamiast z węzła:

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 - Właśnie edytowane do wyprowadzania nodeValue zamiast instancji tekst! – karim79

11

to dlatego an element in fact has no nodeValue. Zamiast tego ma węzeł tekstowy jako dziecko, które ma nodeValue, którego potrzebujesz.

Krótko mówiąc, będziesz chciał getNodeValue() na pierwszym potomku węzła elementu.

Czasami element zawiera wiele węzłów tekstowych, jak oni mają maksymalny rozmiar, w którym to przypadku będzie trzeba coś a la ta, związana ze strony wcześniej:

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

Dobre wyjaśnienie i wielkie dzięki za doskonałą funkcję. Działa jak mistrz. –

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

Jeśli węzeł nie ma dalszych zagnieżdżonych potomków, niż n.getTextContent() działa całkiem dobrze.

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

Można użyć jOOX jako opakowania standardowego DOM, aby uprościć kod.

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

Można też mieć jOOX przekonwertować wartość podwoi się, na przykład:

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