在Java中解析XML标记中的值

在Java中解析XML标记中的值,java,xml,xml-parsing,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="

我试图解析这个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>
当我将getElementByTagName更改为temperature时,它无法识别它,因为temperature标记中有4个值,它不知道要选择哪一个,而且整个temperature标记结构与country标记结构不同

有没有简单的方法可以只解析温度值

编辑: 我已经更改了解析函数,现在是这样的:

    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());
但仍然无法接收任何值

这个答案是基于
通过这种方式,您可以获得温度的
值。
我添加了更多步骤,以便您可以使用调试器检查中间值。
从相同的
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);
        }
}
public void getTemperature()引发SAXException、IOException、ParserConfiguration异常{
字符串xml=“\r\n”+
“\r\n”+
“\r\n”+
“GB\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
"";
Document doc=DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(新的InputSource(新的StringReader(xml));
NodeList errNodes=doc.getElementsByTagName(“当前”);
if(errNodes.getLength()>0){
元素err=(元素)errNodes.item(0);
节点列表temps=err.getElementsByTagName(“温度”);
节点温度=临时项目(0);
NamedNodeMap attrs=temp.getAttributes();
节点val=attrs.getNamedItem(“值”);
字符串strValue=val.getNodeValue();
System.out.println(标准值);
}
}

temperature标记中的值是带有名称的属性,您应该将它们作为标记来读取。@谢谢您的回答,我尝试使用getAttribute(),但无法正确执行。我应该使用getElementsByTagName(“temprature”).item(0).getAttribute(“value”).getTextContent();因为它在这个答案中不起作用,所以它们是这样做的:System.out.println(nodeList.item(x).getAttributes().getNamedItem(“name”).getNodeValue())@胡安,你能检查一下我的编辑吗。我尝试了项(0,1,2)和所有相同的响应。现在我有了这个错误:java.lang.RuntimeException:不可编译的源代码-找不到符号:类NodeCheck我添加到答案的导入。
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);
        }
}