当点击’&’时,DocumentBuilder解析会破坏字符串
我有这个xml:
H & M
并使用此代码解析它:
DocumentBuilder documentBuilder = null; Document document = null;
try {documentBuilder = DocumentBuilderFactory.newInstance()。newDocumentBuilder(); document = documentBuilder.parse(is); } catch(例外e){返回结果; NodeList nl = document.getElementsByTagName(XML_RESPONSE_ROOT); if(nl.getLength()> 0){resp_code = nl.item(0).getAttributes()。getNamedItem(XML_RESPONSE_STATUS).getNodeValue(); if(resp_code.equals(RESP_CODE_OK_SINGLE)){nl = document .getElementsByTagName(XML_RESPONSE_TAG_CONTACT); NodeList values = nl.item(i).getChildNodes();
等等..
当我得到节点值时:node.getNodeValue();
我得到的只是&符号之前的东西,即使&符号被逃脱了
我想得到整个字符串:“H&M”
谢谢
这取决于XML文档的构造方式。 特别是,它可以在“H&M”中具有多个辅助Text节点,而您的代码期望它只是一个。 尝试在获取其值之前使用nodeVariable.normalize()。
根据DOM解析器API:“normalize() – 将此节点下的子树的完整深度中的所有Text节点(包括属性节点)放入只有结构的”正常“forms(例如,元素,注释,处理指令) ,CDATA部分和实体引用)分隔Text节点,即既没有相邻的Text节点也没有空的Text节点…“
找到“name”元素并调用getTextContent()
。