为什么XPATH无法访问此标记的值? (在Java中)

时间:2013-11-29 17:51:10

标签: java xml xpath xml-parsing jdom

我在 XPATH 中相当新,我遇到以下问题:

我有以下代码片段似乎无法正常工作:

                String XML = cdataContent;

                // Crea un nuovo documento XML a partire dal contenuto della
                // precedente sezione CDATA:
                documentXML = builder.build(new StringReader(XML));

                System.out.println(documentXML.toString());
                System.out.println("CDATA Content in XML document:");
                org.jdom.output.XMLOutputter xmlOutputterCDATAContent = new org.jdom.output.XMLOutputter(
                        org.jdom.output.Format.getPrettyFormat());
                xmlOutputter.output(documentXML, System.out);

                // Crea una nuova query XPATH per selezionare il valore del tag id e lo mette dentro l'oggetto AuthResponse:
                xPath = XPath.newInstance("/root/status/id");
                objectElement = (Element) xPath.selectSingleNode(documentXML);
                System.out.println("Y " + objectElement.getValue() + " Y");
                authResponse.setStatusResponse(objectElement.getValue());

                // Crea una nuova query XPATH per selezionare il valore del tag message: e lo mette dentro l'oggetto AuthResponse:
                XPath xPathMessage = XPath.newInstance("/root/status/message");
                Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);
                System.out.println("MESSAGE: " + objectElementMessage.getValue() + " /MESSAGE");
                authResponse.setStatusResponse(objectElementMessage.getValue());
            }
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这是 documentXML 变量的内容:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <status>
    <id>-1</id>
    <message>Login o password errate</message>
  </status>
</root>

我用前面代码的这一行打印出来:

org.jdom.output.XMLOutputter xmlOutputterCDATAContent = new org.jdom.output.XMLOutputter( org.jdom.output.Format.getPrettyFormat());
xmlOutputter.output(documentXML, System.out);

现在我必须创建2个 XPATH查询:第一个必须取 id 标记的值(在这种情况下 -1 值)和第二个必须取值消息标记内的值(在这种情况下,字符串值“登录o密码错误”

好的,正如你在剪贴画中看到的那样,我做了:

1) XPATH QUERY获取id标签VALUE:

xPath = XPath.newInstance("/root/status/id");
objectElement = (Element) xPath.selectSingleNode(documentXML);
System.out.println("Y " + objectElement.getValue() + " Y");

此查询似乎运行良好,因为在Eclipse控制台中打印:

Y -1 Y

(所以我知道-1值是正确的)

2) XPATH查询以显示消息标记VALUE:

然后我这样做:

XPath xPathMessage = XPath.newInstance("/root/status/message");
Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);
System.out.println("MESSAGE: " + objectElementMessage.getValue() + " /MESSAGE");

现在发生了一些奇怪的事情,因为在Eclipse控制台中,打印以下输出:

消息:-1 / MESSAGE

而不是 MESSAGE:登录o密码错误/ MESSAGE 正如我所料(因为这是消息标记内的值)

为什么呢?我错过了什么?我该怎么做才能解决这种情况?

TNX

安德烈

1 个答案:

答案 0 :(得分:2)

 XPath xPathMessage = XPath.newInstance("/root/status/message");
 Element objectElementMessage = (Element) xPath.selectSingleNode(documentXML);

将其更改为

XPath xPathMessage = XPath.newInstance("/root/status/message");
Element objectElementMessage = (Element) xPathMessage.selectSingleNode(documentXML);