首页 > 代码库 > XML解析与XML拼接

XML解析与XML拼接

2015年了。

1、XML解析

这边是用的dom4j方式解析。

解析的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<DBSET>
	<R>
		<C N="TASKID">10001</C> <!-- TASKID -->
		<C N="TYPE">D</C><!-- 查询类型(D/U) -->
	</R>
	<R>
		<C N="TASKID">10002</C> <!-- TASKID -->
		<C N="TYPE">U</C><!-- 查询类型 (D/U) -->
	</R>
</DBSET>

解析XML代码如下:

	/**
	 * @category 解析传入XMl
	 * @param xmldata
	 * @return List<Map<String, String>>
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> xmlData(String xmldata) {
		List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		Document doc = null;
		Map<String, String> map = null;
		try {
			doc = DocumentHelper.parseText(xmldata);
		} catch (Exception e) {
			map = new HashMap<String, String>();
			map.put(FAIL_MEMO, "错误的xml格式数据");
			lists.add(map);
			return lists;
		}
		try {
			Element root = doc.getRootElement();
			//根元素(DBSET)迭代器
			Iterator<Element> iter = root.elementIterator();
			//解析里面XML
			while (iter.hasNext()) {
				map = new HashMap<String, String>();
				Element cns = (Element) iter.next();
				//R元素迭代器
				Iterator<Element> its = cns.elementIterator();

				while (its.hasNext()) {
					Element cn = (Element) its.next();
					//将C元素的属性名和文本值存入Map中
					map.put(cn.attribute(NAME).getValue(), cn.getText());
				}
				lists.add(map);
			}
		} catch (Exception e) {
			map.put("FAIL_MEMO", "错误的xml格式");
			lists.add(map);
		}
		return lists;
	}

最后存入List<Map<String, String>>中的值为:

[{TASKID=10001, TYPE=D}, {TASKID=10002, TYPE=U}]


2、XML拼接

采用自定义类以及对应工具类的方式,具体代码如下:(感谢tangyangbo)

自定义类:

package com.keyman.struts2.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: Element.java
 * @Package com.keyman.struts2.test
 * @Description: TODO XML元素类
 * @author tangyangbo
 * @date 2014-12-11 下午5:32:45
 * @version V1.0
 */
public class Element {
	private String name; // 元素名
	private String nodeText = ""; // 文本值
	private Map<String, String> property = new HashMap<String, String>(); // 属性
	private boolean isleaf = true; // 是否子节点
	private List<Element> child = new ArrayList<Element>(); // 子节点

	public Element(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNodeText() {
		return nodeText;
	}

	public void setNodeText(String nodeText) {
		this.nodeText = nodeText;
	}

	public Map<String, String> getProperty() {
		return property;
	}

	public void setProperty(Map<String, String> property) {
		this.property = property;
	}

	public boolean isIsleaf() {
		return isleaf;
	}

	// 这个方法应该屏蔽
	public void setIsleaf(boolean isleaf) {
		this.isleaf = isleaf;
	}

	public List<Element> getChild() {
		return child;
	}

	public void setChild(List<Element> child) {
		this.child = child;
		if (this.isleaf && this.child.size() > 0) {
			this.isleaf = false;
		}
	}

	/**
	 * 添加属性
	 * 
	 * @param key
	 * @param value
	 */
	public void addProperty(String key, String value) {
		this.property.put(key, value);
	}

	/**
	 * 添加子节点
	 * 
	 * @param el
	 */
	public void addChild(Element el) {
		this.child.add(el);
		if (this.isleaf && this.child.size() > 0) {
			this.isleaf = false;
		}
	}
}

工具类:

package com.keyman.struts2.test;

import java.util.Iterator;

/**
 * @Title: XmlUtil.java
 * @Package com.keyman.struts2.test
 * @Description: TODO XMl工具类
 * @author <span><span class="comment">tangyangbo</span> </span>
 * @date 2014-12-11 下午5:34:02
 * @version V1.0
 */
public class XmlUtil {

	public static String lt = "<";
	public static String ltEnd = "</";
	public static String rt = ">";
	public static String rhtEnd = "/>";
	public static String quotes = "\"";
	public static String equal = "=";
	public static String blank = " ";

	/**
	 * @category 拼接XML各元素信息
	 * @param element
	 * @return
	 */
	public static StringBuffer elementToXml(Element element) {
		StringBuffer result = new StringBuffer();
		// 元素开始
		result.append(lt).append(element.getName());
		// 判断是否有属性
		if (element.getProperty() != null && element.getProperty().size() > 0) {
			Iterator<String> iter = element.getProperty().keySet().iterator();
			while (iter.hasNext()) {
				String key = String.valueOf(iter.next());
				String value = http://www.mamicode.com/element.getProperty().get(key);>
XML拼接测试代码如下:

package com.keyman.struts2.test;

/**
 * @Title: XmlTest.java
 * @Package com.keyman.struts2.test
 * @Description: TODO xml测试类
 * @author <span><span class="comment">tangyangbo</span> </span>
 * @date 2014-12-11 下午5:36:29
 * @version V1.0
 */
public class XMLTest {
	public static void main(String[] args) {
		/**
		 * <?xml version="1.0" encoding="UTF-8"?> 
		 * <DBSET> 
		 * <R> 
		 * <C N="FLAG">0</C><!-- 执行情况-1/0 --> 
		 * <C N="RETURNMESSAGE">message</C><!-- 返回消息 --> 
		 * </R>
		 * </DBSET>
		 */
		Element dbsetElement = new Element("DBSET");

		Element rElement = new Element("R");

		Element cElement1 = new Element("C");
		cElement1.addProperty("N", "FLAG");
		cElement1.setNodeText("0");

		Element cElement2 = new Element("C");
		cElement2.addProperty("N", "RETURNMESSAGE");
		cElement2.setNodeText("message");

		rElement.addChild(cElement1);
		rElement.addChild(cElement2);
		dbsetElement.addChild(rElement);

		System.out.println(XmlUtil.element2Xml(dbsetElement));
	}
}

输出代码如下:

<?xml version="1.0" encoding="UTF-8"?><DBSET><R><C N="FLAG" >0</C><C N="RETURNMESSAGE" >message</C></R></DBSET>

参考资料:http://tyb.iteye.com/blog/1792013


XML解析与XML拼接