8000 修复无法解析<ExtAttr><Item>....</Item></ExtAttr>这种节点数据,如: by giveme0101 · Pull Request #1777 · binarywang/WxJava · GitHub
[go: up one dir, main page]

Skip to content
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
修复无法解析<ExtAttr><Item>....</Item></ExtAttr>这种节点数据,如:
String xml = "<xml><ToUserName><![CDATA[ww3fb6dc50e568348a]]></ToUserName><FromUserName><![CDATA[sys]]></FromUserName><CreateTime>1600855248</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[change_contact]]></Event><ChangeType><![CDATA[update_user]]></ChangeType><UserID><![CDATA[1000100010001]]></UserID><Gender>2</Gender><ExtAttr><Item><Name><![CDATA[工号]]></Name><Value><![CDATA[10007000000]]></Value><Type>0</Type><Text><Value><![CDATA[10007000000]]></Value></Text></Item></ExtAttr><Alias><![CDATA[xiaoqi]]></Alias></xml>";
解析后: ExtAttr== null
修复后:ExtAttr={Item=[{Type=0, Value=10007000000, Text={Value=[10007000000]}, Name=工号}]}
  • Loading branch information
giveme01 committed Sep 24, 2020
commit 75c63a24deccb9273796787ee8b54f1bf8e73400
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