简体   繁体   English

从XML文档Java接收子节点

[英]Receive child node from xml document java

I'm new to xml parsing, but i have to get XML from a specific URL and then save only one resource from it. 我是xml解析的新手,但是我必须从特定的URL获取XML,然后从中仅保存一个资源。

How i get xml: 我如何获取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;
}

This works flawlessly and returns kinda this one document: 这可以正常工作并返回该文件:

<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>

The question is - how could i get only one specified node (for example USD) from all this stuff and save it as new xml? 问题是-我怎么能从所有这些东西中仅获得一个指定的节点(例如USD)并将其另存为新的xml?

Example for output xml: 输出xml的示例:

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

Thanks for help, 感谢帮助,

Cheers 干杯

One way could be to use XPaths to match the required node(s). 一种方法是使用XPath来匹配所需的节点。

import javax.xml.xpath.*;

...

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

If you're sure there is only one node you're looking for, XPath.evaluate can also return single Element objects, but this version with NodeLists will be able to handle scenarios where multiple nodes are returned. 如果您确定只需要一个节点,则XPath.evaluate也可以返回单个Element对象,但是带有NodeLists的该版本将能够处理返回多个节点的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM