简体   繁体   English

xpath给出一个空值的节点

[英]xpath is giving a node with null value

I am using XPath for the first time. 我是第一次使用XPath。 While I am trying to get a node from xml I am getting a node with null value. 当我试图从xml获取节点时,我得到一个具有空值的节点。

here is the xml file look like.. 这是xml文件的样子..

<?xml version="1.0" encoding="UTF-8"?>
<Message>
<Header>
<FromCompany>Content</FromCompany>
<From>Source</From>
<FromEmail>eg@eg.com</FromEmail>
<Toy>Pocket</Toy>
<SentDate>20300709</SentDate>
</Header>

... ...

I have written xpath expr as below ... 我写了xpath expr如下...

public XPath xPath =  XPathFactory.newInstance().newXPath();
Node message = (Node) xPath.evaluate("/Message", pxml, XPathConstants.NODE);
System.out.println("message::" + message);

When I execute message node is not null but is printing data as message::[Message: null] 当我执行消息节点不是null但是打印数据作为消息:: [消息:null]

I am not able to get the content of the node.. Could some one help me to find what I have done wrong? 我无法获得节点的内容..有人可以帮助我找到我做错了吗?

In this context xPath will return a org.w3c.dom.Node , which is expected (I hope). 在这种情况下,xPath将返回一个org.w3c.dom.Node ,这是预期的(我希望)。

To get the contents of the node, you need to use Node#getTextContent , which, using your example, outputs... 要获取节点的内容,您需要使用Node#getTextContent ,使用您的示例输出...

Content
Source
eg@eg.com
Pocket
20300709

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM