2014-03-13 52 views
0
<?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> 

惠在那裏,我想讀這個XML文件並需要得到標籤內的文本給出的特定標籤內:閱讀使用XPath與多個條件的Java

<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 

在此先感謝

+0

可能的重複[使用XPath Java讀取標籤](http://stackoverflow.com/questions/22372533/read-inside-a-tag-using-xpath-java) –

回答

0

試試這個表達你的XPath。

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

據同時顯示屬性標籤與名稱=「說明」 1)根據MODELATTRIBUTES標籤直接訪問和2)實例標籤下

0

試試這個:

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; 

更舉例XPath與Java:

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; 
    } 
+0

我沒有收到你 –

0

我認爲你有什麼是好的,你只需要將/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()); 
} 
0

按照我給出的xml文件我的XPath表達應該是:

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

它的作品賓果!