2012-08-29 32 views
-2

在下面這個例子中,比如說file.xml 標籤裏面有值return code =「」我只需要<Port name="write_qwe">中的值。使用java從xml標記值

<Main display="NORMAL"> 
    <Port name="read_abc" exe="NO"> 
     <input> 
      <struct file="C:\temp" sign="id1"/> 
     </input> 
     <output> 
      <return code="33" shortmsg="Implementation not found for commande."/> 
     </output> 
    </Port> 
    <Port name="write_qwe" exe="NO"> 
     <input> 
      <struct file="C:\temp" id="id1"/> 
     </input> 
     <output> 
      <return code="1" shortmsg="NOTEXECUTED" longmsg="Not execute due to previous error"/> 
     </output> 
    </Port> 
    <Port name="read_abc" exe="NO"> 
     <input> 
      <struct file="C:\temp" sign="id2"/> 
     </input> 
     <output> 
      <return code="66" shortmsg="Implementation"/> 
     </output> 
    </Port> 
    <Port name="write_qwe" exe="NO"> 
     <input> 
      <struct file="C:\temp" id="id2"/> 
     </input> 
     <output> 
      <return code="0" shortmsg="NOTEXECUTED" /> 
     </output> 
    </Port> 
</Main> 

我需要得到的 <return code" "> 的值,它是內部 <port name="write_*"> 和內部 <output>。 在這個例子中,我需要得到值「1」和「0」。

+1

我建議使用'DOM','XPath' – adatapost

+0

試試這個它會幫助你.............. http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom – Sathish

+0

@paul我已經完成了代碼,直到它檢查了「read_abc」或「write_qwe」 – Stella

回答

2

XPath可能是這裏要走的路。

我已經把XML文件作爲資源,但你可能有它在一個文件結構。

​​

然後創建一個XPath表達式。

XPathFactory xPathFactory = XPathFactory.newInstance(); 
XPath xPath = xPathFactory.newXPath(); 
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code"); 
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 

XPath表達式/Main/Port[@name='write_qwe']/output/return/@code會發現所有code屬性,其中Port的屬性namewrite_qwe

現在你可以遍歷這樣的節點:

for (int i = 0; i < nl.getLength(); i++) { 
    Node node = nl.item(i); 
    System.out.println(node.getNodeValue()); 
} 

,如果你想整個<return>節點,而不是你可以限制XPath來/Main/Port[@name='write_qwe']/output/return

並遍歷這樣,而不是:

for (int i = 0; i < nl.getLength(); i++) { 
    Node node = nl.item(i); 
    System.out.println(node.getAttributes().getNamedItem("code").getNodeValue()); 
} 

編輯

正如所建議的comment通過Blaise Doughan它可能是最好使用InputSource作爲輸入XPathExpression#evaluate()代替:

ClassLoader loader = XmlTestReader.class.getClassLoader(); 
InputStream inputStream = loader.getResourceAsStream("test.xml"); 
InputSource inputSource = new InputSource(inputStream); 

XPathFactory xPathFactory = XPathFactory.newInstance(); 
XPath xPath = xPathFactory.newXPath(); 
XPathExpression expr = xPath.compile("/Main/Port[@name='write_qwe']/output/return/@code"); 
NodeList nl = (NodeList) expr.evaluate(inputSource , XPathConstants.NODESET); 
+0

+1 - 但我建議調用'evaluate'方法來代替'InputSource'。這使XPath實現有機會在不需要DOM的情況下構建DOM。這可以導致性能改進。 –

+1

@BlaiseDoughan我給你的答案加了你的建議。謝謝。 – maba

2

如果你沒有在使用XPath任何問題,那麼你可以試試這個:

你必須給在爭論你的XML文件的路徑。

XPathReader reader = new XPathReader("FileName.xml"); 

    // To get a xml attribute. 
    String expression = "/Main/Port/output/@code"; 

    System.out.println(reader.read(expression,XPathConstants.STRING) + "n"); 
0

,如果你的XML是一個字符串,然後你就可以做到以下幾點:

String xml = ""; //Populated XML String.... 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = DocumentBuilderFactory.newDocumentBuilder(); 
Document document = builder.parse(new InputSource(new StringReader(xml))); 
Element rootElement = document.getDocumentElement(); 

如果你的XML是一個文件,那麼文件的文件將被實例化這樣的:

Document document = builder.parse(new File("file.xml")); 

document.getDocumentElement()將返回文檔的文檔元素節點(在您的案例中爲<config>)。

一旦你有一個rootElement的,你可以訪問該元素的屬性(通過調用rootElement.getAttribute()方法)等詳細方法Java的org.w3c.dom.Element

爲了進一步澄清,查看這個鏈接可以幫助你多少。 ..

http://www.java-samples.com/showtutorial.php?tutorialid=152

喝彩..!