如标题
通过URL解析xml文件,xml文件需要下载保存在本地上嘛?还是直接在网页上解析就可以了?我用的是pull解析,用第三方jar包。就是搞不懂解析xml的时候要不要下载下来到本地上,如果要的话好心的大神能给个下载xml的demo嘛?
你应该获取到的是一个xml格式的字符串,然后直接在内存中解析。
http://www.cnblogs.com/zhuawang/p/3371291.html
package com.hql.Util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
Created by Administrator on 2016/10/28.
*/
public class XmlDownloadUtil {
private static XmlDownloadUtil instance;
private XmlDownloadUtil() {
}
public static XmlDownloadUtil getInstance() {
if (null == instance) {
instance = new XmlDownloadUtil();
}
return instance;
}
public String LoadXml(String url) {
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
String line = null;
try {
URL link = new URL(url);
HttpURLConnection connection = (HttpURLConnection) link.openConnection();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
}
是这样子吗?但是为什么我得到字符串为null呢?