使用 DOM 解析 Java XML 以获取节点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1291280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java XML parsing using DOM to get nodevalue
提问by kal
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());
回答by karim79
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);
}
回答by Sebastian Paaske T?rholm
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();
}
回答by adatapost
System.out.println(n.getFirstChild().getNodeValue());
回答by brian_d
If the node has no further nested descendants, than n.getTextContent()
works quite well.
如果节点没有进一步嵌套的后代,则n.getTextContent()
效果很好。
回答by iKushal
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;
}
回答by Lukas Eder
You could use jOOXas 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 value = $(data).find("b").text(Double.class);