xml按照节点排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/x18094/article/details/89524698

 xml按照节点排序

package swing;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class XmlMapUtil{
	/**
	 * xml转map 不带属性
	 * @param xmlStr
	 * @param needRootKey 是否需要在返回的map里加根节点键
	 * @return
	 * @throws DocumentException
	 */
	public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {
		Document doc = DocumentHelper.parseText(xmlStr);
		Element root = doc.getRootElement();
		Map<String, Object> map = (Map<String, Object>) xml2map(root);
		if(root.elements().size()==0 && root.attributes().size()==0){
			return map;
		}
		if(needRootKey){
			//在返回的map里加根节点键(如果需要)
			Map<String, Object> rootMap = new TreeMap<>(new Comparator<String>() {

				@Override
				public int compare(String o1, String o2) {
					return o1.compareTo(o2);
				}
			});
			rootMap.put(root.getName(), map);
			return rootMap;
		}
		return map;
	}

	/**
	 * xml转map 不带属性
	 * @param e
	 * @return
	 */
	private static Map xml2map(Element e) {
		Map map = new TreeMap<>(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		List list = e.elements();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();
 
				if (iter.elements().size() > 0) {
					Map m = xml2map(iter);
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!(obj instanceof List)) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(m);
						}
						if (obj instanceof List) {
							mapList = (List) obj;
							mapList.add(m);
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), m);
				} else {
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!(obj instanceof List)) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(iter.getText());
						}
						if (obj instanceof List) {
							mapList = (List) obj;
							mapList.add(iter.getText());
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), iter.getText());
				}
			}
		} else
			map.put(e.getName(), e.getText());
		return map;
	}

/**
	 * map转xml map中没有根节点的键
	 * @param map
	 * @param rootName
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static Document map2xml(Map<String, Object> map, String rootName) {
		Document doc = DocumentHelper.createDocument();
        Element root = DocumentHelper.createElement(rootName);
        doc.add(root);
        map2xml(map, root);
        //System.out.println(doc.asXML());
        //System.out.println(formatXml(doc));
        return doc;
	}

/**
	 * map转xml
	 * @param map
	 * @param body xml元素
	 * @return
	 */
	private static Element map2xml(Map<String, Object> map, Element body) {
		Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry<String, Object> entry = entries.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			if(key.startsWith("@")){	//属性
				body.addAttribute(key.substring(1, key.length()), value.toString());
			} else if(key.equals("#text")){	//有属性时的文本
				body.setText(value.toString());
			} else {
				if(value instanceof java.util.List ){
					List list = (List)value;
					Object obj;
					for(int i=0; i<list.size(); i++){
						obj = list.get(i);
						//list里是map或String,不会存在list里直接是list的,
						if(obj instanceof java.util.Map){
							Element subElement = body.addElement(key);
							map2xml((Map)list.get(i), subElement);
						} else {
							body.addElement(key).setText((String)list.get(i));
						}
					}
				} else if(value instanceof java.util.Map ){
					Element subElement = body.addElement(key);
					map2xml((Map)value, subElement);
				} else {
					body.addElement(key).setText(value.toString());
				}
			}
			//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		}
		return body;
	}
}

调用:

		Map map1 =	XmlMapUtil.xml2map("xml字符串", false);

		Document doc11 = XmlMapUtil.map2xml(map1, "root");

猜你喜欢

转载自blog.csdn.net/x18094/article/details/89524698