我搜索过这个。我得到的只是xml解析/ Sax解析器。我需要一个可以下载xml数据的程序。 我需要这个用于Android应用程序开发。感谢
例如我有一个网站localhost:8080 / folder / sample.html ..如何从中获取.xml文件?
答案 0 :(得分:2)
试试这段代码:
public String getXmlText(String urlXml) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
String result = null;
try {
url = new URL(urlXml);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
result = result + line + "\n";
}
} catch (Exception e) {
return "";
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {}
}
return result;
}
答案 1 :(得分:1)
很抱歉,如果我没有回答这个问题 - 但这是网站内容,你想下载吗?如果是肯定的,这些是解决方案所在的类似问题:
答案 2 :(得分:0)
试试此代码
import java.net.*;
import java.io.*;
class Crawle {
public static void main(String ar[]) throws Exception {
URL url = new URL("http://www.foo.com/your_xml_file.xml");
InputStream io = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(io));
FileOutputStream fio = new FileOutputStream("file.xml");
PrintWriter pr = new PrintWriter(fio, true);
String data = "";
while ((data = br.readLine()) != null) {
pr.println(data);
}
}
}