private Map<String, String> parseXml2Map(String xml, Map<String, String> map) {
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(new StringReader(xml));
Element root = doc.getRootElement();
String path = "";
if (map.containsKey(root.getName().trim())) {
path = map.get(root.getName().trim());
map.remove(root.getName().trim());
}
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if (element.isTextOnly()) {
if (path.length() > 0) {
map.put(path + element.getName().trim(), element.getTextTrim());
} else {
map.put(element.getName().trim(), element.getTextTrim());
}
} else {
map.put(element.getName().trim(), path+ element.getName().trim() + ".");
parseXml2Map(element.asXML(), map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
java解析xml字符串的值
最新推荐文章于 2024-07-18 14:43:20 发布