从Java中的XML标记中解析值

时间:2018-05-17 16:18:48

标签: java xml xml-parsing

我正在尝试解析此XML响应中的温度值。

<current>
<city id="2643743" name="London">
<coord lon="-0.13" lat="51.51"/>
<country>GB</country>
<sun rise="2017-01-30T07:40:36" set="2017-01-30T16:47:56"/>
</city>
<temperature value="280.15" min="278.15" max="281.15" unit="kelvin"/>
<humidity value="81" unit="%"/>
<pressure value="1012" unit="hPa"/>
<wind>
<speed value="4.6" name="Gentle Breeze"/>
<gusts/>
<direction value="90" code="E" name="East"/>
</wind>
<clouds value="90" name="overcast clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="701" value="mist" icon="50d"/>
<lastupdate value="2017-01-30T15:50:00"/>
</current>

我能够解析国家,例如因为标签内只有一个值,但我想要的只是温度值,在这个例子中是280.15。

我的解析代码是:

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource (new StringReader(response.toString())));

    NodeList errNodes=doc.getElementsByTagName("current");
    if(errNodes.getLength()>0){
    Element err=(Element) errNodes.item(0);
    System.out.println(err.getElementsByTagName("country").item(0).getTextContent());
    }else{
    }

当我将getElementByTagName更改为温度时,它不会识别它,当然因为在温度标签内有4个值,并且它不知道选择哪个并且整个温度标签结构不同来自国家标签结构。

有没有简单的方法来解析温度值?

修改 我已经改变了解析功能,现在它是这样的:

    NodeList errNodes=doc.getElementsByTagName("temprature");
    if(errNodes.getLength()>0){
   // Element err=(Element) errNodes.item(0);
    System.out.println(errNodes.item(2).getAttributes().getNamedItem("value").getNodeValue());

但仍然无法接收任何值!

1 个答案:

答案 0 :(得分:0)

此答案基于this question
通过这种方式,您可以获得value的温度。 我添加了更多步骤,因此您可以使用调试器检查中间值 从同一个attrs开始,您可以获得温度测量单位。

请注意,方法名称具有误导性,因为它不会返回任何内容。唯一的目的是显示访问温度值的代码。

进口:

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

方法:

public void getTemperature() throws SAXException, IOException, ParserConfigurationException {

        String xml = "<current>\r\n" + 
        "<city id=\"2643743\" name=\"London\">\r\n" + 
        "<coord lon=\"-0.13\" lat=\"51.51\"/>\r\n" + 
        "<country>GB</country>\r\n" + 
        "<sun rise=\"2017-01-30T07:40:36\" set=\"2017-01-30T16:47:56\"/>\r\n" + 
        "</city>\r\n" + 
        "<temperature value=\"280.15\" min=\"278.15\" max=\"281.15\" unit=\"kelvin\"/>\r\n" + 
        "<humidity value=\"81\" unit=\"%\"/>\r\n" + 
        "<pressure value=\"1012\" unit=\"hPa\"/>\r\n" + 
        "<wind>\r\n" + 
        "<speed value=\"4.6\" name=\"Gentle Breeze\"/>\r\n" + 
        "<gusts/>\r\n" + 
        "<direction value=\"90\" code=\"E\" name=\"East\"/>\r\n" + 
        "</wind>\r\n" + 
        "<clouds value=\"90\" name=\"overcast clouds\"/>\r\n" + 
        "<visibility value=\"10000\"/>\r\n" + 
        "<precipitation mode=\"no\"/>\r\n" + 
        "<weather number=\"701\" value=\"mist\" icon=\"50d\"/>\r\n" + 
        "<lastupdate value=\"2017-01-30T15:50:00\"/>\r\n" + 
        "</current>";


        Document doc = DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .parse(new InputSource (new StringReader(xml)));

        NodeList errNodes=doc.getElementsByTagName("current");
        if(errNodes.getLength()>0){
            Element err=(Element) errNodes.item(0);
            NodeList temps = err.getElementsByTagName("temperature");
            Node temp = temps.item(0);
            NamedNodeMap attrs = temp.getAttributes();
            Node val = attrs.getNamedItem("value");
            String strValue = val.getNodeValue();
            System.out.println(strValue);
        }
}