English 中文(简体)
Retrive XML XPath的要素
原标题:Retrieve XML Elements with XPath

I m 很难从使用XPath的XML档案中获取XML数值。 The XML file has the following schema:

<devicesInfo>
    <deviceInfo>
    <deviceId>device1</deviceId>
    <macAddress>00:21:85:C9:19:A4</macAddress>
    <deviceType>deviceType1</deviceType>
    <deviceClass>/Server/SSH/Linux/</deviceClass>
    <location>location1</location>
    <productionState>Decommissioned</productionState>
</deviceInfo>
<deviceInfo>
    <deviceId>device2</deviceId>
    <macAddress>00:21:85:C9:19:F5</macAddress>
    <deviceType>deviceType1</deviceType>
    <deviceClass>/Server/SSH/Linux/</deviceClass>
    <location>location1</location>
    <productionState>Decommissioned</productionState>
</deviceInfo>
</devicesInfo>

首先,我试图做的是做些什么,这形成了我所讲的习俗,其中包括以下方法:

  public NodeList getNodesList(String xmlQuery) throws XPathExpressionException {
        NodeList nodes = null;
        try {
            nodes = (NodeList) xPath.evaluate(xmlQuery, doc, XPathConstants.NODESET);
        } catch (XPathExpressionException ex) {
            throw ex;
        }
        return nodes;

    }

这种方法然后被问到:

 NodeList nodes = xmlHandler.getNodesList("/devicesInfo/deviceInfo");

So far so good, for this is returning a NodeList with length 2, which is basically all the deviceInfo elements in the file. What I wish to do next is iterating over each deviceInfo in order to get the value of its children (deviceId,macAddress,deviceType,deviceClass,location,productionState).

I tried to do the following: NodeList list = nodes.item(0).getChildNodes();

之后

 System.out.println("First element is "+list.item(0).getTextContent());

这使我感到空洞。 尽管我试图这样做:

System.out.println("First element is "+list.item(1).getTextContent());

基本上用指数“1”而不是指数“0”显示项目,这就导致印刷“device1”,这是第一个要素的价值。

因此,为了获得所有要素的价值,我使用了指数1,3,5,7,9,11。 我知道我做的是错事。 谁能帮助我?

最佳回答

The children of the <deviceInfo> elements include a number of Whitespace- only text nodes. 你们需要过滤这些内容,以便你仅以下述方式对作为要素的儿童重新定位,或将XML归为一种只有白空间在<代码>和t;deviceInfo>内容为:ignored/

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签