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

    } 

Ben 0.15 yazdırmak için bekliyorum, ancak null yazdırır. Herhangi bir fikir?Java XML ayrıştırma için DOM kullanarak ayrıştırma

Düzenleme:

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); 
} 

cevap

6

Unsuru dan ziyade Düğüm olarak çıkarmayı deneyin hile yaptı. Bunun yerine, istediğiniz gibi nodeValue olan bir çocuk olarak bir metin düğümü vardır.

Özetle, öğenin düğümündeki ilk çocukta getNodeValue() olmasını istersiniz.

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

@mkal - Sadece çıkışa yerine Metin örneğinin NodeValue düzenlenmiş! – karim79

11

an element in fact has no nodeValue çünkü var: Bu

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

İyi bir açıklama ve mükemmel işlev için çok teşekkürler. Bir şampiyon gibi çalışır. –

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

ise: onlar la bu á şey gerekir ki bu durumda maksimum boyutu var gibi sayfa önceki bağlantılı dan

Bazen eleman, birden fazla metin düğümlerini içerir Düğümün iç içe geçmiş torunları yoktur, n.getTextContent()'dan çok iyi çalışır.

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

Kodunuza basitleştirmek için, standart DOM için sarıcı olarak jOOX kullanabilirsiniz.

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

Yapabilirsin da jOOX örneğin iki katına bu değeri dönüştürmek vardır:

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