如何获取节点值?

时间:2013-11-04 05:41:01

标签: java android

在此示例中如何在Android上获取Java中的userid值?

<RESPONSE>
    <SINGLE>
        <KEY name="sitename"></KEY>
        <KEY name="username"></KEY>
        <KEY name="firstname"></KEY>
        <KEY name="lastname"></KEY>
        <KEY name="fullname"></KEY>
        <KEY name="lang"></KEY>
        <KEY name="userid"></KEY>
        <KEY name="siteurl"></KEY>
        <KEY name="userpictureurl"></KEY>
        <KEY name="functions"></KEY>
        <KEY name="downloadfiles"></KEY>
        <KEY name="release"></KEY>
        <KEY name="version"></KEY>
        <KEY name="mobilecssurl"></KEY>
    </SINGLE>
</RESPONSE>

需要输出

[userid value]

1 个答案:

答案 0 :(得分:0)

使用SAX,您可以获得节点值:

    String xPathExpression = "RESPONSE/SINGLE/KEY[@name= 'userid']/text()";
    XPath path = XPathFactory.newInstance().newXPath();
    String result = (String) path.evaluate(xPathExpression, new InputSource(new FileReader("..path to xml file..")), XPathConstants.STRING);