xml字符串轉(zhuǎn)成map對象
public Map<String, String> xmlStrToMap(String xmlStr) throws Exception {
if (StringUtils.isEmpty(xmlStr)) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
// 將xml格式的字符串轉(zhuǎn)換成Document對象
Document doc = DocumentHelper.parseText(xmlStr);
// 獲取根節(jié)點
Element root = doc.getRootElement();
// 獲取根節(jié)點下的所有元素
List children = root.elements();
// 循環(huán)所有子元素
if (children != null && children.size() > 0) {
for (int i = 0; i < children.size(); i++) {
Element child = (Element) children.get(i);
map.put(child.getName(), child.getTextTrim());
}
}
return map;
}
map對象轉(zhuǎn)成bean對象
public Bean mapToBean(Map<String, String> map)
throws ParseException {
Bean be= new Bean ();
be.setName(map.get("Name"));
......
return be;
}