使用具有多个条件 Java 的 XPath 读取特定标签内部

Read inside a Specific Tag using XPath with multiple conditions Java(使用具有多个条件 Java 的 XPath 读取特定标签内部)

本文介绍了使用具有多个条件 Java 的 XPath 读取特定标签内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?>
-<ADOXML adoversion="Version 5.1" username="kvarga" database="adonisdb" time="08:55"   date="30.11.2013" version="3.1">
-<MODELS>
-<MODEL version="" applib="ADONIS BPMS BP Library 5.1" libtype="bp" modeltype="Business process model" name="Product development" id="mod.25602">
 -<MODELATTRIBUTES>
<ATTRIBUTE name="Version number" type="STRING"> </ATTRIBUTE>
<ATTRIBUTE name="Author" type="STRING">kvarga</ATTRIBUTE>
<ATTRIBUTE name="Creation date" type="STRING">2013-11-30, 08:50</ATTRIBUTE>
<ATTRIBUTE name="Date last changed" type="STRING">2013-11-30, 08:54:46</ATTRIBUTE>

-<INSTANCE name="Business Opportunities census" id="obj.25615" class="Activity">
<ATTRIBUTE name="Position" type="STRING">NODE x:6.5cm y:10.5cm index:7</ATTRIBUTE>

<ATTRIBUTE name="External tool coupling" type="STRING"> </ATTRIBUTE>
<ATTRIBUTE name="Description" type="STRING">I WANT THIS PARA 1</ATTRIBUTE>


<ATTRIBUTE name="Version number" type="STRING"> </ATTRIBUTE>
<ATTRIBUTE name="Author" type="STRING">kvarga</ATTRIBUTE>
<ATTRIBUTE name="Creation date" type="STRING">2013-11-30, 08:50</ATTRIBUTE>
<ATTRIBUTE name="Date last changed" type="STRING">2013-11-30, 08:54:46</ATTRIBUTE>

-<INSTANCE name="Business Opportunities census" id="obj.25615" class="Activity">
<ATTRIBUTE name="Position" type="STRING">NODE x:6.5cm y:10.5cm index:7</ATTRIBUTE>
<ATTRIBUTE name="Description" type="STRING">I WANT THIS PARA 2</ATTRIBUTE>
</INSTANCE>


 </MODEL>

 </MODELS>

 </ADOXML>

Hye 我想读取这个 xml 文件,并且需要获取标签内的文本:

Hye There I want to read this xml file and need to get the text inside the tag given as:

 <ATTRIBUTE name="Description" type="STRING">

我一直在尝试使用我的代码获得结果:

I have been Trying to get the results using my code as:

 DocumentBuilderFactory builderFactory =
    DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = null;
try {

builder = builderFactory.newDocumentBuilder();
        org.w3c.dom.Document document = builder.parse(
        new FileInputStream("c:\y.xml"));

        XPath xPath =  XPathFactory.newInstance().newXPath();

       String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'and @type='STRING']";
   System.out.println(expression);
   NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); 
}


} catch (ParserConfigurationException | SAXException | IOException e) {
System.out.print(e);
}       

我的代码有问题,无法弄清楚是什么!

There is a problem with my code cant figure out what!

如果我使用 XPath 表达式,我的代码可以正常工作:

My code works fine if i use XPath expression as:

String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@type='STRING']";

它工作正常,但我要读取的特定标签是:

It works fine but my specific Tag to read from is:

   <ATTRIBUTE name="Description" type="STRING"> I WANT THIS PARA 1 </ATTRIBUTE>

所以输出应该是:

   I WANT THIS PARA 1
   I WANT THIS PARA 2

提前致谢

推荐答案

为你的 XPath 试试这个表达式.

Try this expression for your XPath.

String expression = "//ADOXML//MODELS//MODEL//MODELATTRIBUTES//ATTRIBUTE[@name='Description' and @type='STRING'] | //ADOXML//MODELS//MODEL//MODELATTRIBUTES//INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']";

它同时显示名称 = 'Description' 的 Attributes 标签 1) 在 MODELATTRIBUTES 标签下直接访问和 2) 在 INSTANCE 标签下

It is displaying both Attributes tag with name = 'Description' 1) directly accessed under MODELATTRIBUTES tag and 2) under the INSTANCE tag

这篇关于使用具有多个条件 Java 的 XPath 读取特定标签内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用具有多个条件 Java 的 XPath 读取特定标签内部

基础教程推荐