me.chanjar.weixin.common.util.XmlUtils Maven / Gradle / Ivy
package me.chanjar.weixin.common.util;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultText;
import org.xml.sax.SAXException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* XML转换工具类.
* Created by Binary Wang on 2018/11/4.
*
*
* @author Binary Wang
*/
public class XmlUtils {
public static Map xml2Map(String xmlString) {
Map map = new HashMap<>(16);
try {
SAXReader saxReader = new SAXReader();
saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
saxReader.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
saxReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
saxReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document doc = saxReader.read(new StringReader(xmlString));
Element root = doc.getRootElement();
List elements = root.elements();
for (Element element : elements) {
map.put(element.getName(), element2MapOrString(element));
}
} catch (DocumentException | SAXException e) {
throw new WxRuntimeException(e);
}
return map;
}
private static Object element2MapOrString(Element element) {
final List content = element.content();
final Set names = names(content);
// 判断节点下有无非文本节点(非Text和CDATA),如无,直接取Text文本内容
if (names.size() < 1) {
return element.getText();
}
Map result = Maps.newHashMap();
if (names.size() == 1) {
// 说明是个列表,各个子对象是相同的name
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy