XML和Map相互转换

前端之家收集整理的这篇文章主要介绍了XML和Map相互转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;

import com.alibaba.fastjson.JSONObject;

public class XMLUtil {
	public static void main(String[] args) throws UnsupportedEncodingException,DocumentException {
		String xml = "<xml><return_code>SUCCESS</return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx2421b1c4370ec43b]]></appid><mch_id><![CDATA[10000100]]></mch_id><nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str><sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id><trade_type><![CDATA[JSAPI]]></trade_type></xml>";
		Map<String,Object> map = parseXml2Map(xml);
		System.out.println(JSONObject.toJSON(map));
		System.out.println(parseMap2XML(map,"xml"));

	}

	/**
	 * 将xml转换为map
	 * @param xml
	 * @return
	 */
	public static Map<String,Object> parseXml2Map(String xml) {
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Map<String,Object> map = (Map<String,Object>) xml2map(doc
				.getRootElement());
		return map;
	}

	/**
	 * 将map转换为xml
	 * @param vo
	 * @param rootElement
	 * @return
	 */
	public static String parseMap2XML(Map<String,Object> vo,String rootElement) {
		org.dom4j.Document doc = DocumentHelper.createDocument();
		Element body = DocumentHelper.createElement(rootElement);
		doc.add(body);
		buildMap2xmlBody(body,vo);
		doc.setXMLEncoding("UTF-8");
		// 去掉头部的 <?xml version="1.0" encoding="UTF-8"?> 或者换行
		return doc.asXML().replaceAll("^<\\?xml.*\\?>|\n","");
	}

	private static void buildMap2xmlBody(Element body,Map<String,Object> vo) {
		if (vo != null) {
			Iterator<String> it = vo.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				if (key != null && !key.equals("")) {
					Object obj = vo.get(key);
					Element element = DocumentHelper.createElement(key);
					if (obj != null) {
						if (obj instanceof java.lang.String) {
							element.setText((String) obj);
						} else {
							if (obj instanceof java.lang.Character
									|| obj instanceof java.lang.Boolean
									|| obj instanceof java.lang.Number
									|| obj instanceof java.math.BigInteger
									|| obj instanceof java.math.BigDecimal) {
								org.dom4j.Attribute attr = DocumentHelper
										.createAttribute(element,"type",obj
												.getClass().getCanonicalName());
								element.add(attr);
								element.setText(String.valueOf(obj));
							} else if (obj instanceof java.util.Map) {
								org.dom4j.Attribute attr = DocumentHelper
										.createAttribute(element,java.util.Map.class
														.getCanonicalName());
								element.add(attr);
								buildMap2xmlBody(element,(Map<String,Object>) obj);
							} else {
							}
						}
					}
					body.add(element);
				}
			}
		}
	}

	private static Object xml2map(Element element) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Element> elements = element.elements();
		if (elements.size() == 0) {
			map.put(element.getName(),element.getText());
			if (!element.isRootElement()) {
				return element.getText();
			}
		} else if (elements.size() == 1) {
			map.put(elements.get(0).getName(),xml2map(elements.get(0)));
		} else if (elements.size() > 1) {
			// 多个子节点的话就得考虑list的情况了,比如多个子节点有节点名称相同的
			// 构造一个map用来去重
			Map<String,Element> tempMap = new HashMap<String,Element>();
			for (Element ele : elements) {
				tempMap.put(ele.getName(),ele);
			}
			Set<String> keySet = tempMap.keySet();
			for (String string : keySet) {
				Namespace namespace = tempMap.get(string).getNamespace();
				List<Element> elements2 = element.elements(new QName(string,namespace));
				// 如果同名的数目大于1则表示要构建list
				if (elements2.size() > 1) {
					List<Object> list = new ArrayList<Object>();
					for (Element ele : elements2) {
						list.add(xml2map(ele));
					}
					map.put(string,list);
				} else {
					// 同名的数量不大于1则直接递归去
					map.put(string,xml2map(elements2.get(0)));
				}
			}
		}

		return map;
	}
}
原文链接:https://www.f2er.com/xml/296003.html

猜你在找的XML相关文章