使用DOM获取nodevalue的Java XML解析

时间:2022-12-01 13:38:49
    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);

    }

I am expecting it to print 0.15, but it prints null. Any Ideas?

我希望它打印0.15,但它打印为null。有任何想法吗?

Edit: This did the trick

编辑:这就是诀窍

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

6 个解决方案

#1


11  

That's because an element in fact has no nodeValue. Instead it has a text node as a child, which has the nodeValue you want.

那是因为一个元素实际上没有nodeValue。相反,它有一个文本节点作为子节点,它具有您想要的nodeValue。

In short, you'll want to getNodeValue() on the first child of the element node.

简而言之,您需要在元素节点的第一个子节点上获取getNodeValue()。

Sometimes the element contains multiple text nodes, as they do have a maximum size, in which case you'll need something á la this, from the page linked earlier:

有时元素包含多个文本节点,因为它们具有最大大小,在这种情况下,您需要从以前链接的页面中获取某些内容:

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

#2


7  

Try extracting it from the Element rather than from the Node:

尝试从Element而不是Node中提取它:

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

#3


3  

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

#4


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;

}

#5


1  

If the node has no further nested descendants, than n.getTextContent() works quite well.

如果节点没有进一步嵌套的后代,则n.getTextContent()工作得很好。

#6


1  

You could use jOOX as a wrapper for standard DOM, to simplify your code.

您可以使用jOOX作为标准DOM的包装器,以简化代码。

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

You could also have jOOX convert that value to double, for instance:

您还可以让jOOX将该值转换为double,例如:

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

#1


11  

That's because an element in fact has no nodeValue. Instead it has a text node as a child, which has the nodeValue you want.

那是因为一个元素实际上没有nodeValue。相反,它有一个文本节点作为子节点,它具有您想要的nodeValue。

In short, you'll want to getNodeValue() on the first child of the element node.

简而言之,您需要在元素节点的第一个子节点上获取getNodeValue()。

Sometimes the element contains multiple text nodes, as they do have a maximum size, in which case you'll need something á la this, from the page linked earlier:

有时元素包含多个文本节点,因为它们具有最大大小,在这种情况下,您需要从以前链接的页面中获取某些内容:

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

#2


7  

Try extracting it from the Element rather than from the Node:

尝试从Element而不是Node中提取它:

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

#3


3  

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

#4


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;

}

#5


1  

If the node has no further nested descendants, than n.getTextContent() works quite well.

如果节点没有进一步嵌套的后代,则n.getTextContent()工作得很好。

#6


1  

You could use jOOX as a wrapper for standard DOM, to simplify your code.

您可以使用jOOX作为标准DOM的包装器,以简化代码。

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

You could also have jOOX convert that value to double, for instance:

您还可以让jOOX将该值转换为double,例如:

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