使用JAXP进行XM解析(基于DOM)

最近在做微信开发需要各种解析各种xml,基本用JAXP解析的

JAXP 开发包是J2SE的一部分,它由javax.xml、org.w3c.dom 、org.xml.sax 包及其子包组成。
在 javax.xml.parsers 包中,定义了几个工厂类,通过调用这些工厂类,可以得到对xml文档进行解析的 DOM 或 SAX 的解析器对象。

javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 , DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance方法 ,这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。

1.调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();


2.调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。
DocumentBuilder builder = factory.newDocumentBuilder();


3.调用 DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document 对象,进行可以利用DOM特性对整个XML文档进行操作了

import java.io.IOException;
import java.io.InputStream;
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格式的解析
 */
public class XmlParser {

	/**
	 * xml文件中的数据转换我Map<String,Object>中的数据
	 * 
	 * @param xml
	 *            xml格式的字符串
	 * @return
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 */
	public static Map<String, Object> getMapFromXML(String xml)
			throws ParserConfigurationException, IOException, SAXException {

		// 这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		InputStream is = HttpRequestUtil.getStringStream(xml);//
		Document document = builder.parse(is);
		// 获取到document里面的全部结点
		NodeList allNodes = document.getFirstChild().getChildNodes();
		Node node;
		Map<String, Object> map = new HashMap<String, Object>();
		int i = 0;
		while (i < allNodes.getLength()) {
			node = allNodes.item(i);
			if (node instanceof Element) {
				map.put(node.getNodeName(), node.getTextContent());
			}
			i++;
		}
		return map;
	}
}
	/**
	 * 将字符串转换为输入流
	 * 
	 * @param sInputString
	 *            待转换为输入流的字符串
	 * @return
	 */
	public static InputStream getStringStream(String sInputString) {
		ByteArrayInputStream tInputStringStream = null;
		if (sInputString != null && !sInputString.trim().equals("")) {
			try {
				tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return tInputStringStream;
	}


注:直接用sInputString.getBytes()会出现1 字节的 UTF-8 序列的字节 1 无效 此类问题

猜你喜欢

转载自philjing.iteye.com/blog/2391999