getNodeValue()给出了不需要的值

时间:2017-11-09 11:35:26

标签: java xml dom

我正在尝试解析xml文件,将值存储到Map。

这是我使用的xml。

<devise code="eur">
<waehrung>1 EUR</waehrung>
<kurs>1.17113</kurs>
</devise>

这是我使用的代码。

Map<String, CurrencyAndRate> currencyMap = new TreeMap<>();
    try
    {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        DocumentBuilder b = f.newDocumentBuilder();
        Document doc = b.parse(xmlUrl);
        NodeList items = doc.getElementsByTagName("devise");

        for (int i = 0; i < items.getLength(); i++)
        {
            CurrencyAndRate object = new CurrencyAndRate();

            Node nNode = items.item(i);
            Element eElement = (Element) nNode;
            String codeName = eElement.getAttribute("code");

            NodeList waehrungList = eElement.getElementsByTagName("waehrung");
            Element waehrungElement = (Element) waehrungList.item(0);
            Node waehrungNode = waehrungElement.getChildNodes().item(0);
            object.setCurrency(waehrungNode.getNodeValue());

            NodeList kursList = eElement.getElementsByTagName("kurs");
            Element kursElement = (Element) kursList.item(0);
            Node kursNode = kursElement.getChildNodes().item(0);
            object.setRate(kursNode.getNodeValue());
            currencyMap.put(codeName, object);

        }
    }
    catch (Exception e)
    {
        System.out.println("Exception :" + e.toString());
    }

但问题是我的价值是1.17019而不是1.17113。我该如何解决这个问题?

0 个答案:

没有答案