mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-22 03:27:39 +08:00
🐛 #1777 XML工具类修复无法解析<ExtAttr><Item>这种节点数据的问题
This commit is contained in:
@@ -3,10 +3,7 @@ package me.chanjar.weixin.common.util;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import org.dom4j.Document;
|
import org.dom4j.*;
|
||||||
import org.dom4j.DocumentException;
|
|
||||||
import org.dom4j.Element;
|
|
||||||
import org.dom4j.Node;
|
|
||||||
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.SAXReader;
|
||||||
import org.dom4j.tree.DefaultText;
|
import org.dom4j.tree.DefaultText;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@@ -50,14 +47,16 @@ public class XmlUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Object element2MapOrString(Element element) {
|
private static Object element2MapOrString(Element element) {
|
||||||
Map<String, Object> result = Maps.newHashMap();
|
|
||||||
|
|
||||||
final List<Node> content = element.content();
|
final List<Node> content = element.content();
|
||||||
if (content.size() <= 1) {
|
final Set<String> names = names(content);
|
||||||
|
|
||||||
|
// 判断节点下有无非文本节点(非Text和CDATA),如无,直接取Text文本内容
|
||||||
|
if (names.size() < 1) {
|
||||||
return element.getText();
|
return element.getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Set<String> names = names(content);
|
Map<String, Object> result = Maps.newHashMap();
|
||||||
if (names.size() == 1) {
|
if (names.size() == 1) {
|
||||||
// 说明是个列表,各个子对象是相同的name
|
// 说明是个列表,各个子对象是相同的name
|
||||||
List<Object> list = Lists.newArrayList();
|
List<Object> list = Lists.newArrayList();
|
||||||
@@ -90,7 +89,8 @@ public class XmlUtils {
|
|||||||
private static Set<String> names(List<Node> nodes) {
|
private static Set<String> names(List<Node> nodes) {
|
||||||
Set<String> names = Sets.newHashSet();
|
Set<String> names = Sets.newHashSet();
|
||||||
for (Node node : nodes) {
|
for (Node node : nodes) {
|
||||||
if (node instanceof DefaultText) {
|
// 如果节点类型是Text或CDATA跳过
|
||||||
|
if (node instanceof DefaultText || node instanceof CDATA) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
names.add(node.getName());
|
names.add(node.getName());
|
||||||
|
Reference in New Issue
Block a user