读书人

XML解析 - 转载

发布时间: 2012-08-22 09:50:34 作者: rapoo

XML解析 -- 转载
//连接服务器
public static InputStream openConn(String path) {
HttpURLConnection uc;
URL url;
InputStream is = null;
try {
url = new URL(path);
// SocketAddress addr = new
// InetSocketAddress("10.0.0.172",80);//是代理地址
// Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);
// uc = (HttpURLConnection) url.openConnection(typeProxy);
uc = (HttpURLConnection) url.openConnection();
uc.connect();
is = uc.getInputStream();
//uc.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block

e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}

return is;
}


//XML解析部分
InputStream is = openConn(你要解析的服务器的页面);

//无法连接
if (is == null)
return false;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
Document document = null;
try {
document = builder.parse(is);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}

Element rootElement = document.getDocumentElement();

NodeList list = rootElement.getElementsByTagName("你的XML节点名");

读书人网 >XML SOAP

热点推荐