Dom4J解析XML

说明:本文主要是用dom4j来解析一个相对复杂的xml,也是工作中用到的,在此留一个备份吧,附件中有相应的dom4j所需的Jar文件和事例源码。

 

【样式XML】

<gt:QueryCircleInput xmlns:gml="http://www.opengis.net/gml" xmlns:gt="http://www.sgcc.com.cn/sggis/service/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd">
	<gt:Token>1f69980f-92a0-4101-a7cb-452dd1cdd0cf</gt:Token>
	<gml:CircleCenterPoint num="1">
		<gml:pos>12345 54321</gml:pos>
		<gml:radius uom="mt">0.2</gml:radius>
	</gml:CircleCenterPoint>
	<gt:PWhereArray>
		<gt:Type>11111111111111</gt:Type>
		<gt:WhereClause>Dydj='110'</gt:WhereClause>
	</gt:PWhereArray>
	<gt:PWhereArray>
		<gt:Type>22222222222222</gt:Type>
		<gt:WhereClause>Dydj='220'</gt:WhereClause>
	</gt:PWhereArray>
</gt:QueryCircleInput>

 

【解析源码】

package com.dyc.dom4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseXmlFromDom4J {

	/**
	 * 来解析一个稍微复杂一点的
	 * 
	 * @param args
	 */
	public void parseXml(String objXml) {
		// 1、将传入的字符串流化
		InputStream in = null;
		try {
			in = new ByteArrayInputStream(objXml.getBytes("UTF-8"));
			SAXReader reader = new SAXReader();
			// 2、将流转成document对象
			Document document = reader.read(in);
			Element rootElement = document.getRootElement(); // 拿到根节点
			Element tokenNode = rootElement.element("Token");
			if (tokenNode == null) {
				System.out.println("Token节点获取为空");
			}
			System.out.println("Token--->" + tokenNode.getTextTrim());
			Element circleElement = rootElement.element("CircleCenterPoint");
			/* 3、解析出普通的属性 */
			if (null == circleElement) {
				System.out.println("circleElement节点获取为空");
			}
			System.out.println("numArc--->"
					+ circleElement.attributeValue("numArc"));
			Element posNode = circleElement.element("pos");
			if (null == posNode) {
				System.out.println("pos节点获取为空");
			}
			System.out.println("pos--->" + posNode.getTextTrim());
			Element radiusNode = circleElement.element("radius");
			if (null == radiusNode) {
				System.out.println("radius节点获取为空");
			}
			System.out.println("radius--->" + radiusNode.getTextTrim());
			// 4、解析多次重复的循环
			for (Iterator whereArray = rootElement.elementIterator(); whereArray
					.hasNext();) {
				Element whereArrayElement = (Element) whereArray.next();
				String nodeName = whereArrayElement.getName();
				if (!nodeName.equals("PWhereArray")) { // 如果这个节点不是PWhereArray就跳出。
					continue;
				}
				Element TypeNode = whereArrayElement.element("Type");
				Element WhereClauseNode = whereArrayElement
						.element("WhereClause");
				if (null == TypeNode) {
					System.out.println("Type节点获取为空");
				}
				System.out.println("Type--->"
						+ TypeNode.getTextTrim());
				if (null != WhereClauseNode) { // 因为此节点可以为空,所以取不为空的值
					System.out.println("WhereClause--->"
							+ WhereClauseNode.getTextTrim());
				}
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("流操作异常");
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.out.println("Document对象操作异常");
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("流关闭异常");
				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String ss = "<gt:QueryCircleInput  xmlns:gml=\"http://www.opengis.net/gml\" xmlns:gt=\"http://www.sgcc.com.cn/sggis/service/schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sgcc.com.cn/sggis/service/schema gistypes.xsd\"><gt:Token>**********iorutoeutero***********</gt:Token><gt:CircleCenterPoint num=\"1\"><gml:pos>588123 3265375</gml:pos><gml:radius uom=\"m\">0.2</gml:radius></gt:CircleCenterPoint><gt:PWhereArray><gt:Type>04010300003</gt:Type><gt:WhereClause>Dydj='110'</gt:WhereClause></gt:PWhereArray><gt:PWhereArray><gt:Type>04010100001</gt:Type><gt:WhereClause>Dydj='220'</gt:WhereClause></gt:PWhereArray></gt:QueryCircleInput >";
		ParseXmlFromDom4J pfd = new ParseXmlFromDom4J();
		pfd.parseXml(ss);
	}
}

 

你可能感兴趣的:(xml,dom4j)