2014-03-14 133 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="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); 


} 

回答

0

XPath不與第一場比賽停止;相反,它會返回所有匹配的元素。

你需要弄清楚如何在你想要的方面更具體。也許你只是想要第一個INSTANCE

//INSTANCE[0]/ATTRIBUTE[@name='Description' and @type='STRING'] 

或者可能是特定的描述INSTANCE

//INSTANCE[@id='obj.25615']/ATTRIBUTE[@name='Description' and @type='STRING'] 

但ID可能是很隨機的,所以你要使用的實例,而不是名稱:

//INSTANCE[@name='Business Opportunities census']/ATTRIBUTE[@name='Description' and @type='STRING'] 

注意//將遞歸搜索匹配;如果您有幾個MODEL實例與Business Opportunities census,那麼您需要找出區分它們的方法。

+0

先生,我是新來的XML和XPath! –

+0

我已經知道/ ATTRIBUTE會把我帶到實體的孩子節點謝謝 –

1

雖然你給我一個想法什麼我需要知道,所以我必須設法解決我的問題,感謝上帝保佑你對你的想法試試這個字符串

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