Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我试图使用url xml解析,但看起来我一直得到一个空xml_Java_Xml - Fatal编程技术网

Java 我试图使用url xml解析,但看起来我一直得到一个空xml

Java 我试图使用url xml解析,但看起来我一直得到一个空xml,java,xml,Java,Xml,我试图从一个调用的天气API获取信息,即使当我发出请求时,我得到了一个响应,但是当我试图只得到响应的一个特定部分时,每次都会得到空响应。有人能帮我吗?以下是我的处理程序的代码: package weathercalls; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; public class Handler extends Defau

我试图从一个调用的天气API获取信息,即使当我发出请求时,我得到了一个响应,但是当我试图只得到响应的一个特定部分时,每次都会得到空响应。有人能帮我吗?以下是我的处理程序的代码:

package weathercalls;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class Handler extends DefaultHandler
{

    // Create three array lists to store the data
    public ArrayList<Integer> lows = new ArrayList<Integer>();
    public ArrayList<Integer> highs = new ArrayList<Integer>();
    public ArrayList<String> regions = new ArrayList<String>();


    // Make sure that the code in DefaultHandler's
    // constructor is called:
    public Handler()
    {
        super();
    }


    /*** Below are the three methods that we are extending ***/

    @Override
    public void startDocument()
    {
        System.out.println("Start document");
    }


    @Override
    public void endDocument()
    {
        System.out.println("End document");
    }


    // This is where all the work is happening:
    @Override
    public void startElement(String uri, String name, String qName, Attributes atts)
    {
        if(qName.compareTo("region") == 0)
        {
            String region = atts.getLocalName(0);
            System.out.println("Day: " + region);
            this.regions.add(region);
        }
        if(qName.compareToIgnoreCase("wind_degree") == 0)
        {
            int low =  atts.getLength();
            System.out.println("Low: " + low);
            this.lows.add(low);
        }
        if(qName.compareToIgnoreCase("high") == 0)
        {
            int high = Integer.parseInt(atts.getValue(0));
            System.out.println("High: " + high);
            this.highs.add(high);
        }

    }
}
这是我的控制台打印:

Start document
Day: null
Low: 0
End document
http://api.weatherapi.com/v1/current.xml?key=c2e285e55db74def97f151114201701&q=London
http://api.weatherapi.com/v1/current.xml?key=c2e285e55db74def97f151114201701&q=London
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2471cca7
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser@5fe5c6f
org.xml.sax.InputSource@6979e8cb
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl@763d9750


我认为您试图从XML标记中提取值,如果是这样,那么您就错了。Attributes对象包含特定标记的属性,要获得该值,必须做一些额外的工作。与标记的开始类似,标记的内容和结束也有单独的事件。current_tag变量将跟踪正在处理的当前标记。下面是一个示例代码:

class Handler extends DefaultHandler {

// Create three array lists to store the data
public ArrayList<Integer> lows = new ArrayList<Integer>();
public ArrayList<Integer> highs = new ArrayList<Integer>();
public ArrayList<String> regions = new ArrayList<String>();


// Make sure that the code in DefaultHandler's
// constructor is called:
public Handler() {
    super();
}


/*** Below are the three methods that we are extending ***/

@Override
public void startDocument() {
    System.out.println("Start document");
}


@Override
public void endDocument() {
    System.out.println("End document");
}

//Keeps track of the current tag;
String currentTag = "";

// This is where all the work is happening:
@Override
public void startElement(String uri, String name, String qName, Attributes atts) {
    //Save the current tag being handled
    currentTag = qName;
}

//Detect end tag
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    //Reset it
    currentTag = "";
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    //Rules based on current tag
    switch (currentTag) {
        case "region":
            String region = String.valueOf(ch, start, length);
            this.regions.add(region);
            System.out.println("Day: " + region);
            break;
        case "wind_degree":
            int low = Integer.parseInt(String.valueOf(ch, start, length));
            System.out.println("Low: " + low);
            this.lows.add(low);
            break;
        case "high":
            int high = Integer.parseInt(String.valueOf(ch, start, length));
            System.out.println("High: " + high);
            this.highs.add(high);
            break;
    }
}}
注意:请避免在internet上共享API密钥或密码

class Handler extends DefaultHandler {

// Create three array lists to store the data
public ArrayList<Integer> lows = new ArrayList<Integer>();
public ArrayList<Integer> highs = new ArrayList<Integer>();
public ArrayList<String> regions = new ArrayList<String>();


// Make sure that the code in DefaultHandler's
// constructor is called:
public Handler() {
    super();
}


/*** Below are the three methods that we are extending ***/

@Override
public void startDocument() {
    System.out.println("Start document");
}


@Override
public void endDocument() {
    System.out.println("End document");
}

//Keeps track of the current tag;
String currentTag = "";

// This is where all the work is happening:
@Override
public void startElement(String uri, String name, String qName, Attributes atts) {
    //Save the current tag being handled
    currentTag = qName;
}

//Detect end tag
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    //Reset it
    currentTag = "";
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    //Rules based on current tag
    switch (currentTag) {
        case "region":
            String region = String.valueOf(ch, start, length);
            this.regions.add(region);
            System.out.println("Day: " + region);
            break;
        case "wind_degree":
            int low = Integer.parseInt(String.valueOf(ch, start, length));
            System.out.println("Low: " + low);
            this.lows.add(low);
            break;
        case "high":
            int high = Integer.parseInt(String.valueOf(ch, start, length));
            System.out.println("High: " + high);
            this.highs.add(high);
            break;
    }
}}