因为业务需求,根据相关的xml文件解析出相应的字段,并且还要有层级。代码如下:
用springboot,此处为Server层:
@Service
public class XmlServer {
public static List<Map<String, Object>> xml2map(String paramstr) throws Exception {
List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
// 添加子节点
paramstr = "<root>"+paramstr+"</root>";
// 创建Document对象
Document document = DocumentHelper.parseText(paramstr);
// 获取根节点
Element root = document.getRootElement();
// 递归遍历子节点
mapList = getCildNodes(root);
return mapList;
}
/**
* 递归遍历元素的子节点
* @param node
* @return List<Map<String, Object>>
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> getCildNodes(Element node) {
List<Map<String, Object>> parentEl = new ArrayList<Map<String,Object>>();
List<Element> listElement = node.elements();
for (Element el : listElement) {
Map<String,Object> thisEl = new HashMap<String, Object>();
List<Map<String, Object>> childEl = new ArrayList<Map<String,Object>>();
String elName = el.getName();
String elText = el.getTextTrim();
thisEl.put("nodeName", elName);
thisEl.put("nodeText", elText);
List<Attribute> listAttr = el.attributes();
for(Attribute attr : listAttr) {
String attrName = attr.getName();
String attrVal = attr.getValue();
thisEl.put(attrName,attrVal);
}
childEl=getCildNodes(el);
thisEl.put("childNode", childEl);
parentEl.add(thisEl);
}
return parentEl;
}
}
此处为Controller层
@RestController
@RequestMapping("/api")
public class test {
@PostMapping("/test")
public List<Map<String, Object>> test2(@RequestBody Map<String, String> obj) throws Exception {
String xmlString = "<Result substationId='hhhhy' sessionid='3A18AF' substation='哈哈哈院' allowfilemanager='false' allowdocexternview='false' useclearbackground='true' usetabdocview='false' pnserver='' type='Succeed' method='Login'><Reason code='0' /><PersonInfo><name>张三</name><account>CN=张三/O=HNCERI</account></PersonInfo><modules><module Name='我的' /><module Name='功能模块' /><module Name='电子邮件' /><module Name='检索' /><module Name='通讯录' /></modules><MenuItems><Item id='Jobstodo' count='2' desc='待办工作' moduletype='工作'>待办</Item><Item id='Jobsdoing' count='5' desc='工作' moduletype='我的工作'>在办工作</Item><Item id='Jobshandle' count='0' desc='经办工作' moduletype='我的工作'>经办工作</Item><Item id='Jobshistory' count='0' desc='历史工作' moduletype='我的工作'>历史工作</Item><Item id='Sendfiles' count='0' desc='发文管理' moduletype='我的工作'>发文管理</Item><Item id='Receivedfiles' count='0' desc='收文管理' moduletype='我的工作'>收文管理</Item><Item id='Signfor' count='0' desc='签报管理' moduletype='我的工作'>签报管理</Item><Item id='Supervise' count='0' desc='督办管理' moduletype='功能模块'>督办管理</Item><Item id='Inbox' count='1' desc='收件箱' moduletype='电子邮件'>收件箱</Item><Item id='Sentbox' count='21' desc='发件箱' moduletype='电子邮件'>发件箱</Item><Item id='Draftbox' count='4' desc='草稿箱' moduletype='电子邮件'>草稿箱</Item><Item id='Sendmail' count='4' desc='写邮件' moduletype='电子邮件'>写邮件</Item></MenuItems><oldSystemChange><Folders><Item desc='旧OA系统' id='hhhq' /></Folders></oldSystemChange><ChangeLDRC>1</ChangeLDRC><OAServer id='MOA_WebOffice' /></Result>";
List<Map<String, Object>> res = XmlServer.xml2map(xmlString);
return res;
}
}