【问题标题】:Read inside a Specific Tag using XPath with multiple conditions Java使用具有多个条件 Java 的 XPath 读取特定标签内部
【发布时间】:2014-03-13 09:23:25
【问题描述】:
<?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 文件,并且需要获取标签内的文本:

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

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

 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);
}       

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

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

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

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

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

所以输出应该是:

   I WANT THIS PARA 1
   I WANT THIS PARA 2

提前致谢

【问题讨论】:

标签: java xml xpath xml-parsing


【解决方案1】:

为您的 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 标签下直接访问

【讨论】:

    【解决方案2】:

    我认为你所拥有的一切都很好,你只需要在表达式的末尾添加/text() 即可从元素中获取文本节点。

    此外,当您直接访问文本节点时,您不再需要在节点上调用getFirstChild,如果有多个文本节点,这可能会出现问题!

    您也可以在查找属性值时使用eq 而不是=,因为它们在您的示例中始终是原子的。我还认为,虽然这两种形式在语义上是等价的,但从语法上看,使用两个谓词背靠背而不是 and 看起来更好(至少对我而言 ;-))

    String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name eq 'Description'][@type eq 'STRING']/text()";
    
    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).getNodeValue()); 
    }
    

    【讨论】:

      【解决方案3】:

      根据我给定的 XML 文件,我的 XPath 表达式应该是:

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

      它可以玩宾果游戏!

      【讨论】:

        【解决方案4】:

        试试这个:

        List<String> list = new ArrayList<>();
                    try {
                        XPathExpression expr =
                            xpath.compile(//ATTRIBUTE[@name='description']/text()");
                        NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                        for (int i = 0; i < nodes.getLength(); i++)
                            list.add(nodes.item(i).getNodeValue());
                    } catch (XPathExpressionException e) {
                        e.printStackTrace();
                    }
                    return list;
        

        更多使用 java 的 xpath 示例:

        public static String obtenirTextNodoMultiple(Document doc, String nodo)
          {
            String resultat = "";
            int i = 0;
            boolean trobat = true;
            try
            {
              if (doc != null)
                while ((resultat.equals("")) && (trobat)) {
                  String valor = doc.getElementsByTagName(nodo).item(i).getTextContent();
        
                  if (valor != "") {
                    trobat = false;
                    if (!trobat) resultat = valor;
                  }
                  i++;
                }
            }
            catch (Exception e) {
              e.printStackTrace();
            }
            return resultat;
          }
        
          public static String recValorFiltreFill(String pare*father*, String nodeComparar*node to compare*, String valorAComparar*value to compare*, Document doc, String nodo*node*)
          {
            String resultat = "";
            try {
              XPath xpath = XPathFactory.newInstance().newXPath();
              XPathExpression expr = null;
        
              if (doc != null)
              {
                if ((pare.equals("")) && (valorAComparar.equals(""))) {
                  expr = xpath.compile("//" + nodo);
                }
                else if (valorAComparar.equals("")) {
                  expr = xpath.compile("//" + pare + "/" + nodo);
                }
                else {
                  expr = xpath.compile("//" + pare + "[" + nodeComparar + "='" + valorAComparar + "']/" + nodo);
                }
        
                resultat = (String)expr.evaluate(doc, XPathConstants.STRING);
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
        
            return resultat;
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-25
          • 1970-01-01
          • 2012-05-02
          • 1970-01-01
          • 2018-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多