mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 21:56:28 +08:00
🎨 XmlUtils工具类优化,支持变态微信消息
This commit is contained in:
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -15,6 +14,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -50,8 +50,8 @@ public class XmlUtils {
|
||||
|
||||
private static Object element2MapOrString(Element element) {
|
||||
|
||||
final List<Node> content = element.content();
|
||||
final Set<String> names = names(content);
|
||||
final List<Node> nodes = element.content();
|
||||
final List<String> names = names(nodes);
|
||||
|
||||
// 判断节点下有无非文本节点(非Text和CDATA),如无,直接取Text文本内容
|
||||
if (names.size() < 1) {
|
||||
@@ -59,10 +59,11 @@ public class XmlUtils {
|
||||
}
|
||||
|
||||
Map<String, Object> result = Maps.newHashMap();
|
||||
if (names.size() == 1) {
|
||||
Set<String> distinctNames = Sets.newHashSet(names);
|
||||
if (distinctNames.size() == 1) {
|
||||
// 说明是个列表,各个子对象是相同的name
|
||||
List<Object> list = Lists.newArrayList();
|
||||
for (Node node : content) {
|
||||
for (Node node : nodes) {
|
||||
if (node instanceof DefaultText) {
|
||||
continue;
|
||||
}
|
||||
@@ -73,8 +74,8 @@ public class XmlUtils {
|
||||
}
|
||||
|
||||
result.put(names.iterator().next(), list);
|
||||
} else {
|
||||
for (Node node : content) {
|
||||
} else if (distinctNames.size() == names.size()) {
|
||||
for (Node node : nodes) {
|
||||
if (node instanceof DefaultText) {
|
||||
continue;
|
||||
}
|
||||
@@ -83,13 +84,38 @@ public class XmlUtils {
|
||||
result.put(node.getName(), element2MapOrString((Element) node));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 说明有重复name,但不是全部都相同
|
||||
Map<String, Long> namesCountMap = names.stream().collect(Collectors.groupingBy(a -> a, Collectors.counting()));
|
||||
for (Node node : nodes) {
|
||||
if (node instanceof DefaultText) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (node instanceof Element) {
|
||||
String nodeName = node.getName();
|
||||
if (namesCountMap.get(nodeName) == 1) {
|
||||
result.put(nodeName, element2MapOrString((Element) node));
|
||||
} else {
|
||||
List<Object> values;
|
||||
if (result.containsKey(nodeName)) {
|
||||
values = (List<Object>) result.get(nodeName);
|
||||
} else {
|
||||
values = Lists.newArrayList();
|
||||
result.put(nodeName, values);
|
||||
}
|
||||
|
||||
values.add(element2MapOrString((Element) node));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Set<String> names(List<Node> nodes) {
|
||||
Set<String> names = Sets.newHashSet();
|
||||
private static List<String> names(List<Node> nodes) {
|
||||
List<String> names = Lists.newArrayList();
|
||||
for (Node node : nodes) {
|
||||
// 如果节点类型是Text或CDATA跳过
|
||||
if (node instanceof DefaultText || node instanceof CDATA) {
|
||||
|
||||
Reference in New Issue
Block a user