Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复无法解析<ExtAttr><Item>....</Item></ExtAttr>这种节点数据,如: #1777

Merged
merged 1 commit into from
Sep 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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