Skip to content

Commit

Permalink
🐛 #1777 XML工具类修复无法解析<ExtAttr><Item>这种节点数据的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
giveme0101 authored and binarywang committed Sep 24, 2020
1 parent 807ed7d commit e0c995e
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultText;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -50,14 +47,16 @@ public static Map<String, Object> xml2Map(String xmlString) {
}

private static Object element2MapOrString(Element element) {
Map<String, Object> result = Maps.newHashMap();

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();
}

final Set<String> names = names(content);
Map<String, Object> result = Maps.newHashMap();
if (names.size() == 1) {
// 说明是个列表,各个子对象是相同的name
List<Object> list = Lists.newArrayList();
Expand Down Expand Up @@ -90,7 +89,8 @@ private static Object element2MapOrString(Element element) {
private static Set<String> names(List<Node> nodes) {
Set<String> names = Sets.newHashSet();
for (Node node : nodes) {
if (node instanceof DefaultText) {
// 如果节点类型是Text或CDATA跳过
if (node instanceof DefaultText || node instanceof CDATA) {
continue;
}
names.add(node.getName());
Expand Down

0 comments on commit e0c995e

Please sign in to comment.