-1

在下面的示例中,说 file.xml 标签中有值 return code=" " 我<Port name="write_qwe">只需要里面的值。

<Main display="NORMAL">
    <Port name="read_abc" exe="NO">
        <input>
            <struct file="C:\temp" sign="id1"/>
        </input>
        <output>
            <return code="33" shortmsg="Implementation not found for commande."/>
        </output>
    </Port>
    <Port name="write_qwe" exe="NO">
        <input>
            <struct file="C:\temp" id="id1"/>
        </input>
        <output>
            <return code="1" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/>
        </output>
    </Port>
    <Port name="read_abc" exe="NO">
        <input>
            <struct file="C:\temp" sign="id2"/>
        </input>
        <output>
            <return code="66" shortmsg="Implementation"/>
        </output>
    </Port>
    <Port name="write_qwe" exe="NO">
        <input>
            <struct file="C:\temp" id="id2"/>
        </input>
        <output>
            <return code="0" shortmsg="NOTEXECUTED" />
        </output>
    </Port>
</Main>

我需要获取 <return code" "> which is inside <port name="write_*"> 和 inside 的值<output>。在此示例中,我需要获取值 "1" 和 "0" 。

4

3 回答 3

2

如果你在使用 xpath 时没有任何问题,那么你可以试试这个:

您必须在参数中提供 xml 文件的路径。

   XPathReader reader = new XPathReader("FileName.xml");

   // To get a xml attribute.
   String expression = "/Main/Port/output/@code";

   System.out.println(reader.read(expression,XPathConstants.STRING) + "n");
于 2012-08-29T07:06:38.440 回答
2

XPath 可能是要走的路。

我已将 xml 文件作为资源,但您可以将它放在文件结构中。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
ClassLoader loader = XmlTestReader.class.getClassLoader();
InputStream is = loader.getResourceAsStream("test.xml");
Document doc = builder.parse(is);

然后创建一个 XPath 表达式。

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

XPath 表达式/Main/Port[@name='write_qwe']/output/return/@code将查找'code属性所在的所有属性。Portnamewrite_qwe

现在您可以像这样遍历节点:

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    System.out.println(node.getNodeValue());
}

/Main/Port[@name='write_qwe']/output/return如果您想要整个<return>节点,则可以将 XPath 限制为。

而是像这样迭代:

for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    System.out.println(node.getAttributes().getNamedItem("code").getNodeValue());
}

编辑

正如Blaise Doughan的评论所建议的那样,使用 an作为输入可能会更好:InputSourceXPathExpression#evaluate()

ClassLoader loader = XmlTestReader.class.getClassLoader();
InputStream inputStream = loader.getResourceAsStream("test.xml");
InputSource inputSource = new InputSource(inputStream);

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code");
NodeList nl = (NodeList) expr.evaluate(inputSource , XPathConstants.NODESET);
于 2012-08-29T08:20:01.780 回答
0

如果您的 XML 是一个字符串,那么您可以执行以下操作:

String xml = ""; //Populated XML String....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = DocumentBuilderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
Element rootElement = document.getDocumentElement();

如果您的 XML 在文件中,则 Document 文档将像这样实例化:

Document document = builder.parse(new File("file.xml"));

返回您作为document.getDocumentElement()文档的文档元素的节点(在您的情况下<config>)。

一旦有了 rootElement,就可以访问元素的属性(通过调用 rootElement.getAttribute() 方法)等等。有关 java 的更多方法org.w3c.dom.Element

如需进一步说明,请查看此链接,它对您有很大帮助...

http://www.java-samples.com/showtutorial.php?tutorialid=152

干杯..!

于 2012-08-29T07:21:53.790 回答