这个函数返回来的就是xml的文档
public Document getDoc(String url_){
URL url;
Document doc = null;
try {
url = new URL(url_);
InputStream reader = new BufferedInputStream(url.openStream());
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder bf = factory.newDocumentBuilder(); //用工厂制造出一个文件夹
doc = bf.parse(reader);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
以下就是读取值并的到节点的list
Document doc=comimp.getDoc("http://api.video.sina.com.cn/sinatv/weather/provinces.php");
NodeList items = doc.getElementsByTagName("item");
如果xml中没有数据,则用如下处理
Node fnNode = fnE.getFirstChild();
if( fnNode == null ){ name= "" ;
} else { name = fnNode.getNodeValue();
}