<?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="Vess">
<ATTRIBUTE name="Position" type="STRING">NODE x:6.5cm y:10.5cm index:7</ATTRIBUTE>
<ATTRIBUTE name="Description" type="STRING">I DONT WANT THIS PARA 2</ATTRIBUTE>
</INSTANCE>
</MODEL>
</MODELS>
</ADOXML>
嘿,那裏我是新來的XML閱讀!其實我需要的是閱讀上述那種一個XML文件,以便獲得文本使用xpath查詢讀取xml java
I WANT THIS PARA 1
我需要獲得唯一的這段文字 我的XPath查詢是:
String expression = "/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']";
但我的查詢給我的結果爲:
I WANT THIS PARA 1
I DONT WANT THIS PARA 2 //this line i dont want
所以這個問題是隻屬性標記,其名稱是描述和類型爲字符串,但其paraent標籤應該是實例和它的類中讀應該是活動!
我使用這個代碼:
String expression = "/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
ArrayList<String> text = new ArrayList<String>();
int k;
for (k = 0; k < nodeList.getLength(); k++) {
String txt=nodeList.item(k).getTextContent();
txt=txt.replace("[","").replace("]","").replace("#","").replace(":", "").replace("•", "").replace("\n", " ")
.replace("\u0092", " ").replace("'", "").replace("•", " ").replace("-", " ").replace("’", " ").replace("\n", " ");
text.add(txt);
}
先生,我是新來的XML和XPath! –
我已經知道/ ATTRIBUTE會把我帶到實體的孩子節點謝謝 –