Java 如何从URL填充我的.xml文件?

Java 如何从URL填充我的.xml文件?,java,xml,eclipse,inputstream,Java,Xml,Eclipse,Inputstream,我正在构建一个天气应用程序,需要用URL中的数据填充一个.xml文件 XML文件的URL是,我创建了一个名为NewFile.XML的文件 当我手动下载这个URL数据并将其导入EclipseIDE时,它工作得很好。但就是这样,我不能拥有远程资源的las内容 下面是我正在使用的代码部分: String readXML=null; URL=null; URLConnection urlconn=null; 试一试{ url=新url(“http://vrijeme.hr/hrvatska_n.xml"

我正在构建一个天气应用程序,需要用URL中的数据填充一个.xml文件

XML文件的URL是,我创建了一个名为NewFile.XML的文件

当我手动下载这个URL数据并将其导入EclipseIDE时,它工作得很好。但就是这样,我不能拥有远程资源的las内容

下面是我正在使用的代码部分:

String readXML=null;
URL=null;
URLConnection urlconn=null;
试一试{
url=新url(“http://vrijeme.hr/hrvatska_n.xml");
urlconn=url.openConnection();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
InputStreamReader inst=新的InputStreamReader(urlconn.getInputStream());
BufferedReader bfr=新的BufferedReader(inst);
布尔eof=假;
而(!eof){
readXML=bfr.readLine();
if(readXML==null){
eof=真;
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
FileOutputStream fout=新的FileOutputStream(“NewFile.xml”);
Writer out=新的输出流Writer(fout,“UTF8”);
out.write(readXML);
}捕获(IOZ异常){
System.out.println(“Nešto se sjebalo.”);
}
试一试{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(“NewFile.xml”);//Dokument
NodeList cityList=doc.getElementsByTagName(“GradIme”);
节点cNode=城市列表项(12);
if(cNode.getNodeType()==Node.ELEMENT\u Node){
元素cElement=(元素)cNode;
字符串city=cElement.getTextContent();
System.out.println(“输入法梯度:+城市”);
}
NodeList templast=doc.getElementsByTagName(“Temp”);//元素po nazivu
Node nNode=templast.item(12);//项目编号
if(nNode.getNodeType()==Node.ELEMENT\u Node){//provjeta tipa podataka==ELEMENT
元素EEElement=(元素)nNode;//元素nNode
字符串temperaturea=eElement.getTextContent();//uzima text iz elementa
System.out.println(“temperaturea:+temperaturea+C”);
NodeList vlagaList=doc.getElementsByTagName(“Vlaga”);
节点vNode=vlagaList.item(12);
if(vNode.getNodeType()==Node.ELEMENT\u Node){
元素vElement=(元素)vNode;
字符串vlaga=vElement.getTextContent();
System.out.println(“Vlaga u zraku:+Vlaga+”%);
}
}
}捕获(ParserConfiguration异常| IOException | SAXE异常){
e、 printStackTrace();
}

您可以尝试此代码,它正在使用


您应该设法清理
main
方法:您几乎是在为文件读写分离代码块,这导致
readXml
输入在尝试将其写入目标文件时为
null

下面是您源代码的一个稍加修改的版本:

public static void main(String[] args) {
    String readXML;
    URL url;
    URLConnection urlconn;

    try {
        url = new URL("http://vrijeme.hr/hrvatska_n.xml");
        urlconn = url.openConnection();

        BufferedReader bfr = null;
        InputStreamReader inst = null;
        FileOutputStream fout = null;
        Writer out = null;
        try { // read & write in the same block
            inst = new InputStreamReader(urlconn.getInputStream());
            bfr = new BufferedReader(inst);
            fout = new FileOutputStream("NewFile.xml");
            out = new OutputStreamWriter(fout, "UTF8");
            while ((readXML = bfr.readLine()) != null) {
                out.write(readXML);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally { // cleanup your resources
            if (out != null) {
                out.close();
            }
            if (fout != null) {
                fout.close();
            }
            if (bfr != null) {
                bfr.close();
            }
            if (inst != null) {
                inst.close();
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("NewFile.xml");                                            //Dokument

        NodeList cityList = doc.getElementsByTagName("GradIme");
        Node cNode = cityList.item(12);

        if (cNode.getNodeType() == Node.ELEMENT_NODE) {

            Element cElement = (Element) cNode;
            String city = cElement.getTextContent();
            System.out.println("Ime grada: " + city);
        }

        NodeList tempList = doc.getElementsByTagName("Temp");                                //Element po nazivu
        Node nNode = tempList.item(12);                                                        //Item number

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {                                        // provjeta tipa podataka == Element

            Element eElement = (Element) nNode;                                            // Element nNode
            String temperatura = eElement.getTextContent();                                    //uzima text iz elementa
            System.out.println("Temperatura:" + temperatura + " C");

            NodeList vlagaList = doc.getElementsByTagName("Vlaga");
            Node vNode = vlagaList.item(12);

            if (vNode.getNodeType() == Node.ELEMENT_NODE) {

                Element vElement = (Element) vNode;
                String vlaga = vElement.getTextContent();
                System.out.println("Vlaga u zraku: " + vlaga + "%");
            }
        }
    } catch (ParserConfigurationException | IOException | SAXException e) {
        e.printStackTrace();
    }
}

你的代码有什么问题?你有错误吗?或者有意外的结果?在VrijemeDanas.main(VrijemeDanas.java:68)的java.io.Writer.write处的线程“main”java.lang.NullPointerException中出现异常(未知源代码)。这是OutputStream的一行,当我尝试将数据写入NewFile.xml时,请将异常堆栈添加到主帖子中。发生这种情况的原因是
readXML
null
。您应该注意,这应该是一个实验性的代码片段,如果您需要一个可用于生产的代码,您应该设法清理更多。特别注意资源清理。
public static void main(String[] args) {
    String readXML;
    URL url;
    URLConnection urlconn;

    try {
        url = new URL("http://vrijeme.hr/hrvatska_n.xml");
        urlconn = url.openConnection();

        BufferedReader bfr = null;
        InputStreamReader inst = null;
        FileOutputStream fout = null;
        Writer out = null;
        try { // read & write in the same block
            inst = new InputStreamReader(urlconn.getInputStream());
            bfr = new BufferedReader(inst);
            fout = new FileOutputStream("NewFile.xml");
            out = new OutputStreamWriter(fout, "UTF8");
            while ((readXML = bfr.readLine()) != null) {
                out.write(readXML);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally { // cleanup your resources
            if (out != null) {
                out.close();
            }
            if (fout != null) {
                fout.close();
            }
            if (bfr != null) {
                bfr.close();
            }
            if (inst != null) {
                inst.close();
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("NewFile.xml");                                            //Dokument

        NodeList cityList = doc.getElementsByTagName("GradIme");
        Node cNode = cityList.item(12);

        if (cNode.getNodeType() == Node.ELEMENT_NODE) {

            Element cElement = (Element) cNode;
            String city = cElement.getTextContent();
            System.out.println("Ime grada: " + city);
        }

        NodeList tempList = doc.getElementsByTagName("Temp");                                //Element po nazivu
        Node nNode = tempList.item(12);                                                        //Item number

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {                                        // provjeta tipa podataka == Element

            Element eElement = (Element) nNode;                                            // Element nNode
            String temperatura = eElement.getTextContent();                                    //uzima text iz elementa
            System.out.println("Temperatura:" + temperatura + " C");

            NodeList vlagaList = doc.getElementsByTagName("Vlaga");
            Node vNode = vlagaList.item(12);

            if (vNode.getNodeType() == Node.ELEMENT_NODE) {

                Element vElement = (Element) vNode;
                String vlaga = vElement.getTextContent();
                System.out.println("Vlaga u zraku: " + vlaga + "%");
            }
        }
    } catch (ParserConfigurationException | IOException | SAXException e) {
        e.printStackTrace();
    }
}