Node.getNodeValue() returns java 中为空
Node.getNodeValue() returns null in java
我正在尝试通过计算 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 , null
当 sys_id 显然不为空时。
xpath evaluator returns 正确值。
谁能指出任何错误?
谢谢!
我不是 XPath 专家,但基于 this Stack Overflow article 我能够生成以下正确工作的代码:
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();
XPathExpression expr = xPath.compile("//response/result/sys_id"); // these 2 lines
String str = (String) expr.evaluate(dDoc, XPathConstants.STRING); // are different
System.out.println(str);
输出:
dfcgf34dfg56
您的代码中有一个拼写错误:在解析输入时您使用了一个名为 "resp" 的变量,但您定义了一个名为 "resp1" 的变量。
尽管有错别字:为了捕捉 TextContent 使用 node.getTextContent()
System.out.println(node +": " + node.getNodeName() + ", " + node.getTextContent());
您的代码 return 为 null 的原因是:您正在提取一个 ELEMENT 节点并且 getNodeValue() 的结果取决于节点类型(参见 table in API ) 并且对于 ELEMENT 节点将始终 return null。
我正在尝试通过计算 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 , null
当 sys_id 显然不为空时。
xpath evaluator returns 正确值。
谁能指出任何错误?
谢谢!
我不是 XPath 专家,但基于 this Stack Overflow article 我能够生成以下正确工作的代码:
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();
XPathExpression expr = xPath.compile("//response/result/sys_id"); // these 2 lines
String str = (String) expr.evaluate(dDoc, XPathConstants.STRING); // are different
System.out.println(str);
输出:
dfcgf34dfg56
您的代码中有一个拼写错误:在解析输入时您使用了一个名为 "resp" 的变量,但您定义了一个名为 "resp1" 的变量。
尽管有错别字:为了捕捉 TextContent 使用 node.getTextContent()
System.out.println(node +": " + node.getNodeName() + ", " + node.getTextContent());
您的代码 return 为 null 的原因是:您正在提取一个 ELEMENT 节点并且 getNodeValue() 的结果取决于节点类型(参见 table in API ) 并且对于 ELEMENT 节点将始终 return null。