jav springboot XML转换工具类.

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jeecg.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;

/**
 * <pre>
 * XML转换工具类.
 * Created by Binary Wang on 2018/11/4.
 * </pre>
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
public class XmlUtils {

	public static Map<String, Object> xml2Map(String xmlString) {
		Map<String, Object> 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<Element> 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<Node> content = element.content();
		final Set<String> names = names(content);

		// 判断节点下有无非文本节点(非Text和CDATA),如无,直接取Text文本内容
		if (names.size() < 1) {
			return element.getText();
		}

		Map<String, Object> result = Maps.newHashMap();
		if (names.size() == 1) {
			// 说明是个列表,各个子对象是相同的name
			List<Object> list = Lists.newArrayList();
			for (Node node : content) {
				if (node instanceof DefaultText) {
					continue;
				}

				if (node instanceof Element) {
					list.add(element2MapOrString((Element) node));
				}
			}

			result.put(names.iterator().next(), list);
		} else {
			for (Node node : content) {
				if (node instanceof DefaultText) {
					continue;
				}

				if (node instanceof Element) {
					result.put(node.getName(), element2MapOrString((Element) node));
				}
			}
		}

		return result;
	}

	private static Set<String> names(List<Node> nodes) {
		Set<String> names = Sets.newHashSet();
		for (Node node : nodes) {
			// 如果节点类型是Text或CDATA跳过
			if (node instanceof DefaultText || node instanceof CDATA) {
				continue;
			}
			names.add(node.getName());
		}

		return names;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没毛的刷子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值