【发布时间】:2015-11-04 18:04:15
【问题描述】:
我正在尝试通过评估 xpath 表达式来找到节点值。
String resp="<response><result><phone>1234</phone><sys_id>dfcgf34dfg56</sys_id></result></response>";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
org.w3c.dom.Document dDoc = builder.parse(new InputSource(new ByteArrayInputStream(resp.getBytes("utf-8"))));
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.evaluate("//response/result/sys_id", dDoc, XPathConstants.NODE);
System.out.println(node.getNodeName()+" , "+node.getNodeValue());
当 sys_id 明显不为空时,这会给出输出:sys_id , null。
xpath evaluator 返回正确的值。
谁能指出错误?
谢谢!
【问题讨论】:
-
你的代码看起来不错,但是调试它,
dDoc总是为空,所以sys_id将为空......
标签: java dom xpath xml-parsing