使用dom4j 解析 Map<List<Map<String,Object>>> 结构的xml
时间: 2023-10-14 12:19:10 浏览: 91
好的,可以。首先,需要使用Java中的dom4j库来解析XML。然后,使用以下代码来解析Map<List<Map<String, Object>>>结构的XML:
``Document document = DocumentHelperText(xmlString);
Element root = document.getRootElement();
Map<String, List<Map<String, Object>>> map = new HashMap<>();
for (Iterator j = root.elementIterator(); j.hasNext();) {
Element el = (Element) j.next();
List<Map<String, Object>> list = new ArrayList<>();
for (Iterator i = el.elementIterator(); i.hasNext();) {
Element e = (Element) i.next();
Map<String, Object> attrMap = new HashMap<>();
for (Iterator k = e.attributeIterator(); k.hasNext();) {
Attribute attr = (Attribute) k.next();
attrMap.put(attr.getName(), attr.getValue());
}
List<Object> content = new ArrayList<>();
if (!e.isTextOnly()) {
for (Iterator l = e.elementIterator(); l.hasNext();) {
Element innerEl = (Element) l.next();
Map<String, Object> innerAttrMap = new HashMap<>();
for (Iterator m = innerEl.attributeIterator(); m.hasNext();) {
Attribute innerAttr = (Attribute) m.next();
innerAttrMap.put(innerAttr.getName(), innerAttr.getValue());
}
content.add(innerAttrMap);
}
} else {
content.add(e.getTextTrim());
}
attrMap.put("content", content);
list.add(attrMap);
}
map.put(el.getName(), list);
}
```
这段代码会将解析后的XML存储在一个Map<String, List<Map<String, Object>>>中,其中Map的key为节点名称,value为该节点下的所有子节点的List<Map<String, Object>>。
希望这个回答能够帮助到你。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)