我正在构建一个天气应用程序,需要使用来自 URL 的数据填充 .xml 文件。
XML 文件的 URL 为 http://vrijeme.hr/hrvatska_n.xml ,我创建了一个名为 NewFile.xml 的文件。
当我手动下载此 URL 数据并将其导入到 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();
}
最佳答案
您应该设法清理您的 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();
}
}
关于java - 如何从 URL 填充我的 .xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424708/