2016-10-04 68 views
0

我是新來的XML解析,但我必須從特定的URL獲取XML,然後只保存一個資源。從xml文件接收子節點java

我如何獲得XML:

public Document getXMLFile(String urlToXml) { 
    try { 
     URL url = new URL(urlToXml); 
     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
     Document document = documentBuilder.parse(url.openStream()); 
     return document; 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

這完美的作品還挺返回這個文件:

<CRates xmlns="****" xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="******"> 
    <Date>20161003</Date> 
    <Currencies> 
     <Currency> 
      <ID>AUD</ID> 
      <Rate>1.46380</Rate> 
     </Currency> 
     <Currency> 
      <ID>BGN</ID> 
      <Rate>1.95580</Rate> 
     </Currency> 
     <Currency> 
      <ID>BRL</ID> 
      <Rate>3.64090</Rate> 
     </Currency> 
     <Currency> 
      <ID>CAD</ID> 
      <Rate>1.47020</Rate> 
     </Currency> 
     <Currency> 
      <ID>CHF</ID> 
      <Rate>1.09180</Rate> 
     </Currency> 
     <Currency> 
      <ID>CNY</ID> 
      <Rate>7.49620</Rate> 
     </Currency> 
     <Currency> 
      <ID>CZK</ID> 
      <Rate>27.02100</Rate> 
     </Currency> 
     <Currency> 
      <ID>DKK</ID> 
      <Rate>7.44630</Rate> 
     </Currency> 
     <Currency> 
      <ID>GBP</ID> 
      <Rate>0.87318</Rate> 
     </Currency> 
     <Currency> 
      <ID>HKD</ID> 
      <Rate>8.71450</Rate> 
     </Currency> 
     <Currency> 
      <ID>HRK</ID> 
      <Rate>7.50530</Rate> 
     </Currency> 
     <Currency> 
      <ID>HUF</ID> 
      <Rate>308.18000</Rate> 
     </Currency> 
     <Currency> 
      <ID>IDR</ID> 
      <Rate>14587.14000</Rate> 
     </Currency> 
     <Currency> 
      <ID>ILS</ID> 
      <Rate>4.22280</Rate> 
     </Currency> 
     <Currency> 
      <ID>INR</ID> 
      <Rate>74.76600</Rate> 
     </Currency> 
     <Currency> 
      <ID>JPY</ID> 
      <Rate>113.90000</Rate> 
     </Currency> 
     <Currency> 
      <ID>KRW</ID> 
      <Rate>1237.21000</Rate> 
     </Currency> 
     <Currency> 
      <ID>MXN</ID> 
      <Rate>21.61500</Rate> 
     </Currency> 
     <Currency> 
      <ID>MYR</ID> 
      <Rate>4.62720</Rate> 
     </Currency> 
     <Currency> 
      <ID>NOK</ID> 
      <Rate>8.96250</Rate> 
     </Currency> 
     <Currency> 
      <ID>NZD</ID> 
      <Rate>1.54540</Rate> 
     </Currency> 
     <Currency> 
      <ID>PHP</ID> 
      <Rate>54.17900</Rate> 
     </Currency> 
     <Currency> 
      <ID>PLN</ID> 
      <Rate>4.29330</Rate> 
     </Currency> 
     <Currency> 
      <ID>RON</ID> 
      <Rate>4.45050</Rate> 
     </Currency> 
     <Currency> 
      <ID>RUB</ID> 
      <Rate>70.00100</Rate> 
     </Currency> 
     <Currency> 
      <ID>SEK</ID> 
      <Rate>9.59300</Rate> 
     </Currency> 
     <Currency> 
      <ID>SGD</ID> 
      <Rate>1.53260</Rate> 
     </Currency> 
     <Currency> 
      <ID>THB</ID> 
      <Rate>38.91000</Rate> 
     </Currency> 
     <Currency> 
      <ID>TRY</ID> 
      <Rate>3.38610</Rate> 
     </Currency> 
     <Currency> 
      <ID>USD</ID> 
      <Rate>1.12360</Rate> 
     </Currency> 
     <Currency> 
      <ID>ZAR</ID> 
      <Rate>15.26410</Rate> 
     </Currency> 
    </Currencies> 
</CRates> 

的問題是 - 我怎麼能得到的只有一個指定的節點(例如美元)從所有這些東西,並保存爲新的XML?

示例輸出的xml:

<CRates> 
    <Date>20160321</Date> 
    <ID>USD</ID> 
    <Rate>1.12360</Rate> 
    </Currency> 
</CRates> 

感謝您的幫助,

乾杯

回答

3

的一種方式可以是使用XPaths以匹配所需要的(多個)節點。

import javax.xml.xpath.*; 

... 

XPathFactory xpathFactory = XPathFactory.newInstance(); 
XPath xpath = xpathFactory.newXPath(); 
NodeList nodes = (NodeList) xpath.evaluate("//*[@id='USD']", document, XPathConstants.NODESET); 

如果你肯定有你要找的,XPath.evaluate還可以返回單個元素的對象只有一個節點,但這個版本的NodeLists將能夠處理在其中返回多個節點的場景。

+2

這是正確的答案。 XPath就是爲此而設計的。值得花時間學習。 – VGR

+0

感謝您的建議,我會試一試 – Reborn

+0

不客氣。如果您需要使用NodeList的幫助,請閱讀本頁面以瞭解該類(及其相關類)的工作方式。 https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html。讓我知道是否還有其他問題。 – dahui