DocumentBuilder解析在点击'& amp''时会中断字符串 [英] DocumentBuilder parsing breaks string when hits '&'
问题描述
我有这个xml:
< user>
< name> H& amp; amp; M< / name>
,并使用以下代码解析:
and i parse it using this code:
DocumentBuilder documentBuilder = null;
Document document = null;
etc ..
当我得到节点值由:node.getNodeValue();
when i get the node value by: node.getNodeValue();
我只得到了&符号之前的东西,即使&符号被转义了
i get only what's before the ampersand, even though the ampersand is escaped
我想得到整个字符串:H& M
i want to get the whole string: "H & M"
谢谢
推荐答案
取决于您的XML文档的构造方式。特别地,它可以在H& M中具有多个辅助文本节点,而您的代码期望它只是一个。尝试在获取其值之前使用nodeVariable.normalize()。
It depends on how your XML document was constructed. In particular, it can have multiple adjucent Text nodes in "H & M" while your code expects it to be just one. Try to use nodeVariable.normalize() before getting its value.
根据DOM解析器API:normalize() - 将此节点下的子树的全深度中的所有Text节点(包括属性节点)放入正常形式,其中只有结构(例如,元素,注释,处理指令,CDATA部分和实体引用)分离文本节点,即既不存在相邻的文本节点也不存在空文本节点...
According to DOM parser API: "normalize() - Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes..."
这篇关于DocumentBuilder解析在点击'& amp''时会中断字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!