zoukankan      html  css  js  c++  java
  • XML字符串转为Map

    import java.io.ByteArrayInputStream;

    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

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

    public class XMLToMapUtil {

    public static Map<String, String> xmlToMap(String xml, String charset)
    throws UnsupportedEncodingException, DocumentException {

    Map<String, String> respMap = new HashMap<String, String>();

    SAXReader reader = new SAXReader();
    Document doc = reader.read(new ByteArrayInputStream(xml.getBytes(charset)));
    Element root = doc.getRootElement();
    xmlToMap(root, respMap);
    return respMap;
    }

    public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap) {

    if (tmpElement.isTextOnly()) {
    respMap.put(tmpElement.getName(), tmpElement.getText());
    return respMap;
    }

    @SuppressWarnings("unchecked")
    Iterator<Element> eItor = tmpElement.elementIterator();
    while (eItor.hasNext()) {
    Element element = eItor.next();
    xmlToMap(element, respMap);
    }
    return respMap;
    }
    }

  • 相关阅读:
    JAVA 主函数(主方法)
    JAVA 什么时候使用静态
    JAVA 静态成员 static
    JAVA 构造方法之间的调用
    JAVA 构造代码块
    JAVA 构造方法
    JAVA 方法重载
    JAVA 理解封装的概念,private私有的,public公有的
    JAVA this关键字
    JAVA toString方法
  • 原文地址:https://www.cnblogs.com/helloworld-yjh/p/10491118.html
Copyright © 2011-2022 走看看