2016-01-13 55 views
-4

我的.xml文檔以獲得以xml一種元素的標籤的數量包含數據是這樣的:如何使用XML或Java代碼

<Test-Set> 
<Test-Case Name="Verify using Interface user in WebService we are able to do database transactions" /> 
<Test-Case Name="Verify using Interface user in JMS we are able to post a message to queue and get message from queue" /> 
<Test-Case Name="Verify using Interface user we are able to login BPD"/> 
</Test-Set> 

我的預期的輸出是:3

+1

請顯示您寫的代碼,您期望得到的結果,並描述您遇到的問題。否則,我們無法幫助你。請參閱[這裏](http://stackoverflow.com/help/mcve)以獲取關於創建最小,完整和可驗證示例的更多信息,這將有助於我們回答您的問題。 – Sam

+0

你嘗試過什麼嗎? –

+0

看看http://stackoverflow.com/help/how-to-ask,否則你會得到downvotes – SSH

回答

0

未測試:

public class Demo { 

    public static void main(String[] args) throws Exception { 
     String xml = "<Test-Set> " + 
        "<Test-Case Name=\"Verify using Interface user in WebService we are able to do database transactions\" />" + 
        "<Test-Case Name=\"Verify using Interface user in JMS we are able to post a message to queue and get message from queue\" />" + 
        "<Test-Case Name=\"Verify using Interface user we are able to login BPD\"/>" + 
        "</Test-Set>"; 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

     DocumentBuilder builder = factory.newDocumentBuilder(); 

     Document document = builder.parse(new ByteArrayInputStream(xml.getBytes())); 
     String xpath = "count(Test-Set/TestCase)"; 
     XPath xPath = XPathFactory.newInstance().newXPath(); 
     Double s = (Double) xPath.evaluate(xpath, document, XPathConstants.NUMBER); 
    } 

}