1.代码块package com.quickmanager.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 解析xml文档,包括本地文档和url
* @author cyxl
* @version 1.0 2012-05-24
* @since 1.0
*
*/
public class XmlParser {
InputStream inStream;
Element root;
public InputStream getInStream() {
return inStream;
}
public void setInStream(InputStream inStream) {
this.inStream = inStream;
}
public Element getRoot() {
return root;
}
public void setRoot(Element root) {
this.root = root;
}
public XmlParser() {
}
public XmlParser(InputStream inStream) {
if (inStream != null) {
this.inStream = inStream;
DocumentBuilderFactory domfac = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder domBuilder = domfac.newDocumentBuilder();
Document doc = domBuilder.parse(inStream);
root = doc.getDocumentElement();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public XmlParser(String path) {
InputStream inStream = null;
try {
inStream = new FileInputStream(path);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
if (inStream != null) {
this.inStream = inStream;
DocumentBuilderFactory domfac = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder domBuilder = domfac.newDocumentBuilder();
Document doc = domBuilder.parse(inStream);
root = doc.getDocumentElement();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public XmlParser(URL url) {
InputStream inStream = null;
try {
inStream = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
if (inStream != null) {
this.inStream = inStream;
DocumentBuilderFactory domfac = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder domBuilder = domfac.newDocumentBuilder();
Document doc = domBuilder.parse(inStream);
root = doc.getDocumentElement();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @param nodes
* @return 单个节点多个值以分号分隔
*/
public Map getValue(String[] nodes) {
if (inStream == null || root==null) {
return null;
}
Map map = new HashMap();
// 初始化每个节点的值为null
for (int i = 0; i < nodes.length; i++) {
map.put(nodes[i], null);
}
// 遍历第一节点
NodeList topNodes = root.getChildNodes();
if (topNodes != null) {
for (int i = 0; i < topNodes.getLength(); i++) {
Node book = topNodes.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE) {
for (int j = 0; j < nodes.length; j++) {
for (Node node = book.getFirstChild(); node != null; node = node
.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals(nodes[j])) {
//String val=node.getFirstChild().getNodeValue();
String val = node.getTextContent();
System.out.println(nodes[j] + ":" + val);
// 如果原来已经有值则以分号分隔
String temp = map.get(nodes[j]);
if (temp != null && !temp.equals("")) {
temp = temp + ";" + val;
} else {
temp = val;
}
map.put(nodes[j], temp);
}
}
}
}
}
}
}
return map;
}
}2.测试代码
public static void main(String[] args) {
String link = "http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";
URL url;
String path = "test.xml";
try {
url = new URL(link);
System.out.println(url);
// InputStream inStream= url.openStream();
// InputStream inStream=new FileInputStream(new File("test.xml"));
XmlParser parser = new XmlParser(url);
String[] nodes = {"status1","temperature1","temperature2"};
Map map = parser.getValue(nodes);
System.out.println(map.get(nodes[0]));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}3.结果
http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0
status1:阵雨
temperature1:21
temperature2:18
阵雨
转载学习作品,如若得罪版主。sorry。
有需要的话以下有原链接!