在Java中用Dom4j把这一段xml内容转成Map对象:<?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_26" class="java.beans.XMLDecoder"> <object class="java.util.HashMap"> <void method="put"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method="put"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method="put"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method="put"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method="put"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method="put"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method="put"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method="put"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method="put"> <string>att_20160511130031927equg</string> <string></string> </void> <void method="put"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method="put"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>
时间: 2023-07-09 22:10:05 浏览: 91
xmlParse:使用 dom4j 将 XML 文档解析为 Java 对象
您可以使用以下代码将该XML转换为Map对象:
```java
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlToMap {
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <java version=\"1.6.0_26\" class=\"java.beans.XMLDecoder\"> <object class=\"java.util.HashMap\"> <void method=\"put\"> <string>att_20160719180419281NEk9</string> <string>cx</string> </void> <void method=\"put\"> <string>att_20160511094248589ZKYq</string> <string>cnmm</string> </void> <void method=\"put\"> <string>att_20161018153614188aTus</string> <string>zsx;fj</string> </void> <void method=\"put\"> <string>att_20160422110906494XuEf</string> <null/> </void> <void method=\"put\"> <string>att_20160511130210612UULE</string> <string>fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy</string> </void> <void method=\"put\"> <string>att_20160719180542840LnFm</string> <string></string> </void> <void method=\"put\"> <string>att_20160511130025203ssle</string> <string>zdgsszta</string> </void> <void method=\"put\"> <string>att_20160810131359346N0ra</string> <string>szjys</string> </void> <void method=\"put\"> <string>att_20160511130031927equg</string> <string></string> </void> <void method=\"put\"> <string>att_20160422110456036qp7Y</string> <null/> </void> <void method=\"put\"> <string>att_20160422110543585f7iI</string> <string>cpjbxx</string> </void> </object> </java>";
Map<String, String> map = xmlToMap(xml);
System.out.println(map);
}
public static Map<String, String> xmlToMap(String xml) throws Exception {
Map<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement().element("object");
for (Element element : root.elements("void")) {
Element keyElement = element.element("string");
Element valueElement = element.elements().get(1);
String key = keyElement.getText();
String value = valueElement == null ? null : valueElement.getText();
map.put(key, value);
}
return map;
}
}
```
该代码使用Dom4j库解析XML,并将其转换为Map对象。输出结果为:
```
{att_20160719180419281NEk9=cx, att_20160511094248589ZKYq=cnmm, att_20161018153614188aTus=zsx;fj, att_20160422110906494XuEf=null, att_20160511130210612UULE=fjyxapp;jjgjapp;wx;jd;dxjg;tb;wsjy, att_20160719180542840LnFm=, att_20160511130025203ssle=zdgsszta, att_20160810131359346N0ra=szjys, att_20160511130031927equg=, att_20160422110456036qp7Y=null, att_20160422110543585f7iI=cpjbxx}
```
阅读全文