解析xml报文的body内容
/**
* 解析body
* @param s 报文内容
* @return
* @throws DocumentException
*/
public List<Map<String,Object>> dom(String s) throws DocumentException {
// 如果报文最前面有8位数字,先去掉
String substring = s.substring(8);
Document document = DocumentHelper.parseText(substring);
Element rootElement = document.getRootElement();
Iterator<Element> body = rootElement.elementIterator("body");
List<Map<String,Object>> list_ = new ArrayList<>();
while (body.hasNext()){
Element next = body.next();
Iterator<Element> data = next.elementIterator("data");
while (data.hasNext()){
Map<String,Object> map = new HashMap<>();
Element next_ = data.next();
String name = next_.attribute("name").getValue();
String value = next_.elementTextTrim("field");
List<Node> list = next_.selectNodes("array/struct");
List<Map<String,Object>> listMapFile = new ArrayList<>();
for (Node node :list) {
List<Node> list1 = DocumentHelper.parseText(node.asXML()).selectNodes("struct/data/field");
Map<String,Object> fileMap = new HashMap<>();
for (int i = 0; i < list1.size(); i++) {
Node next1 = list1.get(i);
String fileName = next1.getParent().valueOf("@name");
String fileValue = next1.getText();
fileMap.put(fileName,fileValue);
}
listMapFile.add(fileMap);
}
if (list.size()>0){
map.put(name,listMapFile);
}else {
map.put(name,value);
}
list_.add(map);
}
}
return list_;
}