XML格式字符串转换成Map,包括List

import  org. dom4j. DocumentHelper;
import  org. dom4j. io. OutputFormat;
import  org. dom4j. io. XMLWriter;
import  java. io. ByteArrayOutputStream;
import  java. util. ArrayList;
import  java. util. HashMap;
import  java. util. Iterator;
import  java. util. List;
import  java. util. Map;
/**
 * Created by wpj on 2017/5/8.
 */
public  class  XMLFormatUtil {
     /***
      *XMLMAP,List
      *  @param  xml
      *  @return
      */
     public  static  Map  xmlToMap( String  xml) {
         try {
              Map  map  =  new  HashMap();
              org. dom4j. Document  document  =  DocumentHelper. parseText( xml);
              org. dom4j. Element  nodeElement  =  document. getRootElement();
              List  node  =  nodeElement. elements();
              for ( Iterator  it  =  node. iterator();  it. hasNext();) {
                   org. dom4j. Element  elm  = ( org. dom4j. Elementit. next();
                   map. put( elm. getName(),  elm. getText());
                   elm  =  null;
             }
              node  =  null;
              nodeElement  =  null;
              document  =  null;
              return  map;
        }  catch ( Exception  e) {
              e. printStackTrace();
        }
         return  null;
    }
     /***
      * XMLlist
      *  @param  xml
      *  @return
      */
     public  static  List  xmlToList( String  xml) {
         try {
              List < Map >  list  =  new  ArrayList < Map >();
              org. dom4j. Document  document  =  DocumentHelper. parseText( xml);
              org. dom4j. Element  nodesElement  =  document. getRootElement();
              List  nodes  =  nodesElement. elements();
              for ( Iterator  its  =  nodes. iterator();  its. hasNext();) {
                   org. dom4j. Element  nodeElement  = ( org. dom4j. Elementits. next();
                   Map  map  =  xmlToMap( nodeElement. asXML());
                   list. add( map);
                   map  =  null;
             }
              nodes  =  null;
              nodesElement  =  null;
              document  =  null;
              return  list;
        }  catch ( Exception  e) {
              e. printStackTrace();
        }
         return  null;
    }
     /***
      * XMLmap,List
      *  @param  xml
      *  @return
      */
     public  static  Map  xmlToListMap( String  xml) {
         try {
              Map  reMap  =  new  HashMap();
              org. dom4j. Document  document  =  DocumentHelper. parseText( xml);
              org. dom4j. Element  nodesElement  =  document. getRootElement();
              List  nodes  =  nodesElement. elements();
              for ( Iterator  its  =  nodes. iterator();  its. hasNext();) {
                   org. dom4j. Element  nodeElement  = ( org. dom4j. Elementits. next();
                   if( nodeElement. isTextOnly()){
                        reMap. put( nodeElement. getName(),  nodeElement. getText());
                  } else{
                        List < Map >  reList  =  xmlToList( nodeElement. asXML());
                        reMap. put( nodeElement. getName(), reList);
                        reList  =  null;
                  }
             }
              nodes  =  null;
              nodesElement  =  null;
              document  =  null;
              return  reMap;
        }  catch ( Exception  e) {
              e. printStackTrace();
        }
         return  null;
    }
     /***
      * ListxmlString
      *  @param  list
      *  @param  rootName
      *  @return
      */
     public  static  String  list2xml( List <? >  list, String  rootName) {
         org. dom4j. Document  document  =  org. dom4j. DocumentHelper. createDocument();
         org. dom4j. Element  nodesElement  =  document. addElement( rootName);
         list2xml( list, nodesElement);
         return  doc2String( document);
    }
     /***
      * listelementxmlString
      *  @param  list
      *  @param  element
      *  @return
      */
     public  static  org. dom4j. Element  list2xml( List  list, org. dom4j. Element  element){
         int  i  =  0;
         for ( Object  o :  list) {
              org. dom4j. Element  nodeElement  =  element. addElement( "Map");
              if ( o  instanceof  Map) {
                   //nodeElement.addAttribute("type", "o");
                   Map  m  = ( Mapo;
                   for ( Iterator  iterator  =  m. entrySet(). iterator();  iterator. hasNext();) {
                        Map. Entry  entry  = ( Map. Entryiterator. next();
                        org. dom4j. Element  keyElement  =  nodeElement. addElement( entry. getKey(). toString());
                        if ( entry. getValue()  instanceof  List) {
                             //keyElement.addAttribute("type", "l");
                             list2xml(( Listentry. getValue(), keyElement);
                       }  else {
                             //keyElement.addAttribute("type", "s");
                             keyElement. setText( entry. getValue(). toString());
                       }
                  }
             }  else  if ( o  instanceof  List) {
                   //nodeElement.addAttribute("type", "l");
                   list2xml(( List) o, nodeElement);
             }
              else {
                   org. dom4j. Element  keyElement  =  nodeElement. addElement( "value");
                   keyElement. addAttribute( "num"String. valueOf( i));
                   keyElement. setText( String. valueOf( o));
             }
              i ++;
        }
         return  element;
    }
     /***
      * String
      *  @param  document
      *  @return
      */
     public  static  String  doc2String( org. dom4j. Document  document) {
         String  s  =  "";
         try {
              // 使
              ByteArrayOutputStream  out  =  new  ByteArrayOutputStream();
              // 使UTF-8
              OutputFormat  format  =  new  OutputFormat();
              format. setSuppressDeclaration( true);
              XMLWriter  writer  =  new  XMLWriter( outformat);
              writer. write( document);
              s  =  out. toString( "UTF-8");
        }  catch ( Exception  ex) {
              ex. printStackTrace();
        }
         return  s;
    }
}

你可能感兴趣的:(XML格式字符串转换成Map,包括List)