在下面的例子中,假设是file.xml
标签内有值返回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" ">
在里面
<port name="write_*">
在里面
<output>
。
在这个例子中,我需要得到值“1”和“0”。
答案 0 :(得分: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");
答案 1 :(得分: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
的属性Port
为name
的所有write_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
节点,可以将XPath限制为<return>
。
反复迭代:
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
System.out.println(node.getAttributes().getNamedItem("code").getNodeValue());
}
修改强>
根据comment Blaise Doughan的建议,最好使用InputSource
作为XPathExpression#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);
答案 2 :(得分:0)
如果您的XML是String,那么您可以执行以下操作:
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 = 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
欢呼声..!