如何从 URL 填充我的 .xml 文件?
How to fill my .xml file from URL?
我正在构建一个天气应用程序,我需要用 URL[ 中的数据填充 .xml 文件=28=].
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();
}
你可以试试这段代码,它使用了 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");
您应该设法清理您的 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();
}
}
我正在构建一个天气应用程序,我需要用 URL[ 中的数据填充 .xml 文件=28=].
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();
}
你可以试试这段代码,它使用了 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");
您应该设法清理您的 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();
}
}