1 建立网络连接,获取xml输入流
URL url = new URL(urladdr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
connection.setConnectTimeout(20000);
InputStream inStream = connection.getInputStream();
最后别忘了关闭输入流和连接
inStream.close();
connection.disconnect();
在这中间进行XML解析
2 解析
<span style="white-space:pre"> </span>HashMap<String, String> hashMap = new HashMap<String, String>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inStream);
Element root = document.getDocumentElement();
NodeList childNodes = root.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++)
{
Node childNode = (Node) childNodes.item(j);
if (childNode.getNodeType() == Node.ELEMENT_NODE)
{
Element childElement = (Element) childNode;
hashMap.put(childElement.getNodeName(),childElement.getFirstChild().getNodeValue());
}
}
return hashMap;
这里用到的事宜hashMap来存储内容。