我正在构建一个天气应用程序,我需要使用 URL 中的数据填充 .xml 文件。
XML 文件的网址是 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();
}
答案 0 :(得分:1)
您可以尝试使用此代码,它使用的是Java 8 nio API。
<script src= "http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
答案 1 :(得分:1)
您应该设法清理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();
}
}
时目标文件。
这里有一个稍微修改过的源代码版本:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = your-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"