本文共 6671 字,大约阅读时间需要 22 分钟。
/*** xml转map工具类
*@authorzhangyao
**/
public classXmlToMapUtil {/*** xml转map 不带属性
*@paramxmlStr
*@paramneedRootKey 是否需要在返回的map里加根节点键
*@return*@throwsDocumentException*/
public static Map xml2map(String xmlStr, boolean needRootKey) throwsDocumentException {
Document doc=DocumentHelper.parseText(xmlStr);
Element root=doc.getRootElement();
Map map = (Map) xml2map(root);if(root.elements().size()==0 && root.attributes().size()==0){returnmap;
}if(needRootKey){//在返回的map里加根节点键(如果需要)
Map rootMap = new HashMap();
rootMap.put(root.getName(), map);returnrootMap;
}returnmap;
}/*** xml转map 带属性
*@paramxmlStr
*@paramneedRootKey 是否需要在返回的map里加根节点键
*@return*@throwsDocumentException*/
public static Map xml2mapWithAttr(String xmlStr, boolean needRootKey) throwsDocumentException {
Document doc=DocumentHelper.parseText(xmlStr);
Element root=doc.getRootElement();
Map map = (Map) xml2mapWithAttr(root);if(root.elements().size()==0 && root.attributes().size()==0){return map; //根节点只有一个文本内容
}if(needRootKey){//在返回的map里加根节点键(如果需要)
Map rootMap = new HashMap();
rootMap.put(root.getName(), map);returnrootMap;
}returnmap;
}/*** xml转map 不带属性
*@parame
*@return
*/
private staticMap xml2map(Element e) {
Map map= newLinkedHashMap();
List list=e.elements();if (list.size() > 0) {for (int i = 0; i < list.size(); i++) {
Element iter=(Element) list.get(i);
List mapList= newArrayList();if (iter.elements().size() > 0) {
Map m=xml2map(iter);if (map.get(iter.getName()) != null) {
Object obj=map.get(iter.getName());if (!(obj instanceofList)) {
mapList= newArrayList();
mapList.add(obj);
mapList.add(m);
}if (obj instanceofList) {
mapList=(List) obj;
mapList.add(m);
}
map.put(iter.getName(), mapList);
}elsemap.put(iter.getName(), m);
}else{if (map.get(iter.getName()) != null) {
Object obj=map.get(iter.getName());if (!(obj instanceofList)) {
mapList= newArrayList();
mapList.add(obj);
mapList.add(iter.getText());
}if (obj instanceofList) {
mapList=(List) obj;
mapList.add(iter.getText());
}
map.put(iter.getName(), mapList);
}elsemap.put(iter.getName(), iter.getText());
}
}
}elsemap.put(e.getName(), e.getText());returnmap;
}/*** xml转map 带属性
*@parame
*@return
*/
private staticMap xml2mapWithAttr(Element element) {
Map map = new LinkedHashMap();
List list =element.elements();
List listAttr0 = element.attributes(); //当前节点的所有属性的list
for(Attribute attr : listAttr0) {
map.put("@" +attr.getName(), attr.getValue());
}if (list.size() > 0) {for (int i = 0; i < list.size(); i++) {
Element iter=list.get(i);
List mapList= newArrayList();if (iter.elements().size() > 0) {
Map m=xml2mapWithAttr(iter);if (map.get(iter.getName()) != null) {
Object obj=map.get(iter.getName());if (!(obj instanceofList)) {
mapList= newArrayList();
mapList.add(obj);
mapList.add(m);
}if (obj instanceofList) {
mapList=(List) obj;
mapList.add(m);
}
map.put(iter.getName(), mapList);
}elsemap.put(iter.getName(), m);
}else{
List listAttr = iter.attributes(); //当前节点的所有属性的list
Map attrMap = null;boolean hasAttributes = false;if (listAttr.size() > 0) {
hasAttributes= true;
attrMap= new LinkedHashMap();for(Attribute attr : listAttr) {
attrMap.put("@" +attr.getName(), attr.getValue());
}
}if (map.get(iter.getName()) != null) {
Object obj=map.get(iter.getName());if (!(obj instanceofList)) {
mapList= newArrayList();
mapList.add(obj);//mapList.add(iter.getText());
if(hasAttributes) {
attrMap.put("#text", iter.getText());
mapList.add(attrMap);
}else{
mapList.add(iter.getText());
}
}if (obj instanceofList) {
mapList=(List) obj;//mapList.add(iter.getText());
if(hasAttributes) {
attrMap.put("#text", iter.getText());
mapList.add(attrMap);
}else{
mapList.add(iter.getText());
}
}
map.put(iter.getName(), mapList);
}else{//map.put(iter.getName(), iter.getText());
if(hasAttributes) {
attrMap.put("#text", iter.getText());
map.put(iter.getName(), attrMap);
}else{
map.put(iter.getName(), iter.getText());
}
}
}
}
}else{//根节点的
if (listAttr0.size() > 0) {
map.put("#text", element.getText());
}else{
map.put(element.getName(), element.getText());
}
}returnmap;
}/*** map转xml map中没有根节点的键
*@parammap
*@paramrootName
*@throwsDocumentException
*@throwsIOException*/
public static Document map2xml(Map map, String rootName) throwsDocumentException, IOException {
Document doc=DocumentHelper.createDocument();
Element root=DocumentHelper.createElement(rootName);
doc.add(root);
map2xml(map, root);//System.out.println(doc.asXML());//System.out.println(formatXml(doc));
returndoc;
}/*** map转xml map中含有根节点的键
*@parammap
*@throwsDocumentException
*@throwsIOException*/
public static Document map2xml(Map map) throwsDocumentException, IOException {
Iterator> entries =map.entrySet().iterator();if(entries.hasNext()){ //获取第一个键创建根节点
Map.Entry entry =entries.next();
Document doc=DocumentHelper.createDocument();
Element root=DocumentHelper.createElement(entry.getKey());
doc.add(root);
map2xml((Map)entry.getValue(), root);//System.out.println(doc.asXML());//System.out.println(formatXml(doc));
returndoc;
}return null;
}/*** map转xml
*@parammap
*@parambody xml元素
*@return
*/
private static Element map2xml(Mapmap, Element body) {
Iterator> entries =map.entrySet().iterator();while(entries.hasNext()) {
Map.Entry entry =entries.next();
String key=entry.getKey();
Object value=entry.getValue();if(key.startsWith("@")){ //属性
body.addAttribute(key.substring(1, key.length()), value.toString());
}else if(key.equals("#text")){ //有属性时的文本
body.setText(value.toString());
}else{if(value instanceofjava.util.List ){
List list=(List)value;
Object obj;for(int i=0; i
obj=list.get(i);//list里是map或String,不会存在list里直接是list的,
if(obj instanceofjava.util.Map){
Element subElement=body.addElement(key);
map2xml((Map)list.get(i), subElement);
}else{
body.addElement(key).setText((String)list.get(i));
}
}
}else if(value instanceofjava.util.Map ){
Element subElement=body.addElement(key);
map2xml((Map)value, subElement);
}else{
body.addElement(key).setText(value.toString());
}
}//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}returnbody;
}/*** 格式化输出xml
*@paramdocument2
*@return*@throwsDocumentException
*@throwsIOException*/
public static String formatXml(String xmlStr) throwsException {
org.dom4j.Document document=DocumentHelper.parseText(xmlStr);returnformatXml1(document);
}/*** 格式化输出xml
*@paramdocument
*@return*@throwsDocumentException
*@throwsIOException*/
public static String formatXml1(Document document) throwsException {//格式化输出格式
OutputFormat format =OutputFormat.createPrettyPrint();//format.setEncoding("UTF-8");
StringWriter writer = newStringWriter();//格式化输出流
XMLWriter xmlWriter = newXMLWriter(writer, format);//将document写入到输出流
xmlWriter.write(document);
xmlWriter.close();returnwriter.toString();
}
}
转载地址:http://bfcpo.baihongyu.com/