2017-09-26 48 views
1

我正在建立一個天氣應用程序,我需要填寫一個.xml文件,其數據來自網址如何從URL填充我的.xml文件?

XML文件的URL是http://vrijeme.hr/hrvatska_n.xml,我創建了一個名爲NewFile.xml文件。

當我下載這個URL數據manualy並導入到Eclipse IDE,它工作正常。但是是這樣的,我不能擁有遠程資源的las內容。

下面是我使用的代碼部分:

String readXML = null; 
URL url = null; 
URLConnection urlconn = null; 

try { 
    url = new URL("http://vrijeme.hr/hrvatska_n.xml"); 
    urlconn = url.openConnection(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

try { 
    InputStreamReader inst = new InputStreamReader(urlconn.getInputStream()); 
    BufferedReader bfr = new BufferedReader(inst); 
    boolean eof = false; 
    while (!eof) { 
     readXML = bfr.readLine(); 
     if (readXML == null) { 
      eof = true; 
     } 
    } 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

try { 
    FileOutputStream fout = new FileOutputStream("NewFile.xml"); 
    Writer out = new OutputStreamWriter(fout, "UTF8"); 
    out.write(readXML); 
} catch (IOException z) { 
    System.out.println("Nešto se sjebalo."); 
} 

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(); 
} 
+0

什麼是你的代碼錯誤?你會得到錯誤?或有意想不到的結果? – talex

+0

異常線程 「main」 顯示java.lang.NullPointerException \t在java.io.Writer.write(來源不明) \t在VrijemeDanas.main(VrijemeDanas.java:68) –

+0

這是我得到的。當我嘗試將數據寫入NewFile.xml時,它與OutputStream一致。 –

回答

0

你應該設法清除您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(); 
    } 
} 
+0

您應該注意,這應該是一個實驗性代碼片段,如果您需要生產就緒代碼,則應該設法清理更多。 特別注意資源清理。 – tmarwen

1

你可以試試這個代碼,它使用Java 8 nio API

public void downloadFile(final String url, final String targetPath) throws IOException { 

    final URL website = new URL(url); 
    final Path target = Paths.get(targetPath); 
    System.out.println(target.toAbsolutePath().toString()); 
    try (InputStream in = website.openStream()) { 
     Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING); 
    } 
} 

//You can call the function 
downloadFile("http://vrijeme.hr/hrvatska_n.xml", "NewFile.xml");