public static List<HashMap<String,String>> readXml(InputStream inputStream,String nodeName){
//创建一个解析XML工厂对象
SAXParserFactory spf = SAXParserFactory.newInstance();
MyHandler myHandler = new MyHandler(nodeName);
try {
SAXParser parser = spf.newSAXParser();
parser.parse(inputStream, myHandler);
inputStream.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return myHandler.getList();
}
}
HttpUtils.java
package com.sax.http;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtils {
public HttpUtils() {
}
public static InputStream getXML(String path){
InputStream inputStream = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
inputStream = conn.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
}
Test.java
package com.sax.test;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sax.http.HttpUtils;
import com.sax.service.SaxService;
public class Test {
public Test() {
super();
}
/**
* @param args
*/
public static void main(String[] args) {