在 JAVA 中使用 SAX 解析器解析 XML 时,我无法获取 XML 中的数据。 问题是节点是否包含带有某些 unicode 字符的文本数据。
node.getTextContent()
正在按 unicode 字符分割内容并修剪两个 unicode 字符之间的空格。
假设,如果节点有数据 oro-maxilo-facială și implantologie
。
请注意 ă și
之间的空格.
方法node.getTextContent()
返回字符串为 oro-maxilo-facialăși implantologie
(没有空格)。
下面是我尝试过的代码。
private String getNodeContent(Element nodeToSerialize) {
StringBuffer sb = new StringBuffer();
if (nodeToSerialize.hasChildNodes()) {
NodeList nodeList = nodeToSerialize.getChildNodes();
for (int x = 0; x < nodeList.getLength(); x++) {
Node node = nodeList.item(x);
sb.append(node.getTextContent());
}
}
return sb.toString();
}
XML内容是
<record>
<isbn>1234-5689</isbn>
<titles>
<title>Revista de chirurgie oro-maxilo-facială și implantologie</title>
</titles>
<number>16</number>
</record>
最佳答案
问题出在digester1.8上。使用 commons-digester1.8.1.jar 而不是 commons-digester1.8.jar。这将解决这个空白吞咽问题。
关于java - 如何从 XML 节点获取文本而不修剪两个 unicode 字符之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9109236/