com.github.ipaas.ifw.util.XmlUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.github.ipaas.ifw.util.XmlUtil.java

Source

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.ipaas.ifw.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

/**
 * xml 
 *
 * @author Chenql
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class XmlUtil {

    /**
     * xml?map
     * 
     * @param xml
     *            -- xml
     * @return -- map
     */
    public static Map toMap(String xml) {

        ByteArrayInputStream sin = new ByteArrayInputStream(xml.getBytes());
        return toMap(sin);
    }

    /**
     * xml??map
     * 
     * @param in
     *            -- xml?
     * @return -- map
     */
    public static Map toMap(InputStream in) {

        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(in);
            return toMap(doc.getRootElement());
        } catch (Exception ex) {
            throw new RuntimeException("?xml?map", ex);
        }
    }

    /**
     * dom4jElement?map
     * 
     * @param elem
     *            -- dom4j Element
     * @return -- map
     */
    public static Map toMap(Element elem) {

        if (null == elem) {
            throw new IllegalArgumentException("XML Element , ?null");
        }
        List<Element> elems = elem.elements();
        Map tar = new LinkedHashMap();
        for (Element item : elems) {
            generateMap(tar, item);
        }
        return tar;
    }

    /**
     * , dom4j Element???map
     * 
     * @param container
     *            -- ?dom4j Elementmap
     * @param elem
     *            -- dom4j Element
     * @return -- map
     */
    private static Map generateMap(Map container, Element elem) {

        String name = elem.getName();
        Object obj = container.get(name);
        // ????
        if (null != obj) {
            if (!List.class.isInstance(obj)) { // ??,?List(List)
                List<Object> newBean = new LinkedList<Object>();
                newBean.add(obj);
                container.put(name, newBean);
                generateMap(container, elem);
            } else { // ??,List(?elem?List)
                List<Object> bean = (List<Object>) obj;
                if (elem.isTextOnly()) {
                    bean.add(elem.getStringValue());
                } else {
                    List<Element> subs = elem.elements();
                    Map nodes = new LinkedHashMap();
                    bean.add(nodes);
                    for (Element item : subs) {
                        generateMap(nodes, item);
                    }
                }
            }
            return container;
        }
        // ?????
        if (elem.isTextOnly()) { // ?xml,?
            container.put(name, elem.getStringValue());
        } else {
            List<Element> subs = elem.elements();
            Map nodes = new LinkedHashMap();
            container.put(name, nodes);
            for (Element item : subs) {
                generateMap(nodes, item);
            }
        }
        return container;
    }

    /**
     * xml?map
     * 
     * @param xml
     *            -- xml
     * @param encode
     *            -- ?
     * @return -- map
     */
    public static Map toMap(String xml, String encode) throws UnsupportedEncodingException {
        ByteArrayInputStream sin = new ByteArrayInputStream(xml.getBytes(encode));
        return toMap(sin);
    }
}