ava xml轉map map轉xml 帶屬性

參考效果:xml轉json

java xml轉json json轉xml
http://happyqing.iteye.com/blog/2316142

 

java xml轉map

Java代碼  收藏代碼
  1. package xml2map;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.StringWriter;  
  6. import java.util.ArrayList;  
  7. import java.util.HashMap;  
  8. import java.util.Iterator;  
  9. import java.util.LinkedHashMap;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12.   
  13. import net.sf.json.JSON;  
  14. import net.sf.json.JSONObject;  
  15.   
  16. import org.apache.commons.io.FileUtils;  
  17. import org.dom4j.Attribute;  
  18. import org.dom4j.Document;  
  19. import org.dom4j.DocumentException;  
  20. import org.dom4j.DocumentHelper;  
  21. import org.dom4j.Element;  
  22. import org.dom4j.io.OutputFormat;  
  23. import org.dom4j.io.XMLWriter;  
  24.   
  25. /** 
  26.  * xml轉map,map轉xml 帶屬性  
  27.  * http://happyqing.iteye.com/blog/2316275  
  28.  * @author happyqing 
  29.  * @since 2016.8.8 
  30.  */  
  31. @SuppressWarnings({ "rawtypes""unchecked" })  
  32. public class XmlUtil {  
  33.     public static void main(String[] args) throws DocumentException, IOException {  
  34.         String textFromFile = FileUtils.readFileToString(new File("D:/workspace/workspace_3.7/xml2map/src/xml2json/sample.xml"),"UTF-8");  
  35.         Map<String, Object> map = xml2map(textFromFile, false);  
  36.         // long begin = System.currentTimeMillis();  
  37.         // for(int i=0; i<1000; i++){  
  38.         // map = (Map<String, Object>) xml2mapWithAttr(doc.getRootElement());  
  39.         // }  
  40.         // System.out.println("耗時:"+(System.currentTimeMillis()-begin));  
  41.         JSON json = JSONObject.fromObject(map);  
  42.         System.out.println(json.toString(1)); // 格式化輸出  
  43.           
  44.         Document doc = map2xml(map, "root");  
  45.         //Document doc = map2xml(map); //map中含有根節點的鍵  
  46.         System.out.println(formatXml(doc));  
  47.     }  
  48.           
  49.     /** 
  50.      * xml轉map 不帶屬性 
  51.      * @param xmlStr 
  52.      * @param needRootKey 是否需要在返回的map里加根節點鍵 
  53.      * @return 
  54.      * @throws DocumentException 
  55.      */  
  56.     public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {  
  57.         Document doc = DocumentHelper.parseText(xmlStr);  
  58.         Element root = doc.getRootElement();  
  59.         Map<String, Object> map = (Map<String, Object>) xml2map(root);  
  60.         if(root.elements().size()==0 && root.attributes().size()==0){  
  61.             return map;  
  62.         }  
  63.         if(needRootKey){  
  64.             //在返回的map里加根節點鍵(如果需要)  
  65.             Map<String, Object> rootMap = new HashMap<String, Object>();  
  66.             rootMap.put(root.getName(), map);  
  67.             return rootMap;  
  68.         }  
  69.         return map;  
  70.     }  
  71.   
  72.     /** 
  73.      * xml轉map 帶屬性 
  74.      * @param xmlStr 
  75.      * @param needRootKey 是否需要在返回的map里加根節點鍵 
  76.      * @return 
  77.      * @throws DocumentException 
  78.      */  
  79.     public static Map xml2mapWithAttr(String xmlStr, boolean needRootKey) throws DocumentException {  
  80.         Document doc = DocumentHelper.parseText(xmlStr);  
  81.         Element root = doc.getRootElement();  
  82.         Map<String, Object> map = (Map<String, Object>) xml2mapWithAttr(root);  
  83.         if(root.elements().size()==0 && root.attributes().size()==0){  
  84.             return map; //根節點只有一個文本內容  
  85.         }  
  86.         if(needRootKey){  
  87.             //在返回的map里加根節點鍵(如果需要)  
  88.             Map<String, Object> rootMap = new HashMap<String, Object>();  
  89.             rootMap.put(root.getName(), map);  
  90.             return rootMap;  
  91.         }  
  92.         return map;  
  93.     }  
  94.   
  95.     /** 
  96.      * xml轉map 不帶屬性 
  97.      * @param e 
  98.      * @return 
  99.      */  
  100.     private static Map xml2map(Element e) {  
  101.         Map map = new LinkedHashMap();  
  102.         List list = e.elements();  
  103.         if (list.size() > 0) {  
  104.             for (int i = 0; i < list.size(); i++) {  
  105.                 Element iter = (Element) list.get(i);  
  106.                 List mapList = new ArrayList();  
  107.   
  108.                 if (iter.elements().size() > 0) {  
  109.                     Map m = xml2map(iter);  
  110.                     if (map.get(iter.getName()) != null) {  
  111.                         Object obj = map.get(iter.getName());  
  112.                         if (!(obj instanceof List)) {  
  113.                             mapList = new ArrayList();  
  114.                             mapList.add(obj);  
  115.                             mapList.add(m);  
  116.                         }  
  117.                         if (obj instanceof List) {  
  118.                             mapList = (List) obj;  
  119.                             mapList.add(m);  
  120.                         }  
  121.                         map.put(iter.getName(), mapList);  
  122.                     } else  
  123.                         map.put(iter.getName(), m);  
  124.                 } else {  
  125.                     if (map.get(iter.getName()) != null) {  
  126.                         Object obj = map.get(iter.getName());  
  127.                         if (!(obj instanceof List)) {  
  128.                             mapList = new ArrayList();  
  129.                             mapList.add(obj);  
  130.                             mapList.add(iter.getText());  
  131.                         }  
  132.                         if (obj instanceof List) {  
  133.                             mapList = (List) obj;  
  134.                             mapList.add(iter.getText());  
  135.                         }  
  136.                         map.put(iter.getName(), mapList);  
  137.                     } else  
  138.                         map.put(iter.getName(), iter.getText());  
  139.                 }  
  140.             }  
  141.         } else  
  142.             map.put(e.getName(), e.getText());  
  143.         return map;  
  144.     }  
  145.   
  146.     /** 
  147.      * xml轉map 帶屬性 
  148.      * @param e 
  149.      * @return 
  150.      */  
  151.     private static Map xml2mapWithAttr(Element element) {  
  152.         Map<String, Object> map = new LinkedHashMap<String, Object>();  
  153.   
  154.         List<Element> list = element.elements();  
  155.         List<Attribute> listAttr0 = element.attributes(); // 當前節點的所有屬性的list  
  156.         for (Attribute attr : listAttr0) {  
  157.             map.put("@" + attr.getName(), attr.getValue());  
  158.         }  
  159.         if (list.size() > 0) {  
  160.   
  161.             for (int i = 0; i < list.size(); i++) {  
  162.                 Element iter = list.get(i);  
  163.                 List mapList = new ArrayList();  
  164.   
  165.                 if (iter.elements().size() > 0) {  
  166.                     Map m = xml2mapWithAttr(iter);  
  167.                     if (map.get(iter.getName()) != null) {  
  168.                         Object obj = map.get(iter.getName());  
  169.                         if (!(obj instanceof List)) {  
  170.                             mapList = new ArrayList();  
  171.                             mapList.add(obj);  
  172.                             mapList.add(m);  
  173.                         }  
  174.                         if (obj instanceof List) {  
  175.                             mapList = (List) obj;  
  176.                             mapList.add(m);  
  177.                         }  
  178.                         map.put(iter.getName(), mapList);  
  179.                     } else  
  180.                         map.put(iter.getName(), m);  
  181.                 } else {  
  182.   
  183.                     List<Attribute> listAttr = iter.attributes(); // 當前節點的所有屬性的list  
  184.                     Map<String, Object> attrMap = null;  
  185.                     boolean hasAttributes = false;  
  186.                     if (listAttr.size() > 0) {  
  187.                         hasAttributes = true;  
  188.                         attrMap = new LinkedHashMap<String, Object>();  
  189.                         for (Attribute attr : listAttr) {  
  190.                             attrMap.put("@" + attr.getName(), attr.getValue());  
  191.                         }  
  192.                     }  
  193.   
  194.                     if (map.get(iter.getName()) != null) {  
  195.                         Object obj = map.get(iter.getName());  
  196.                         if (!(obj instanceof List)) {  
  197.                             mapList = new ArrayList();  
  198.                             mapList.add(obj);  
  199.                             // mapList.add(iter.getText());  
  200.                             if (hasAttributes) {  
  201.                                 attrMap.put("#text", iter.getText());  
  202.                                 mapList.add(attrMap);  
  203.                             } else {  
  204.                                 mapList.add(iter.getText());  
  205.                             }  
  206.                         }  
  207.                         if (obj instanceof List) {  
  208.                             mapList = (List) obj;  
  209.                             // mapList.add(iter.getText());  
  210.                             if (hasAttributes) {  
  211.                                 attrMap.put("#text", iter.getText());  
  212.                                 mapList.add(attrMap);  
  213.                             } else {  
  214.                                 mapList.add(iter.getText());  
  215.                             }  
  216.                         }  
  217.                         map.put(iter.getName(), mapList);  
  218.                     } else {  
  219.                         // map.put(iter.getName(), iter.getText());  
  220.                         if (hasAttributes) {  
  221.                             attrMap.put("#text", iter.getText());  
  222.                             map.put(iter.getName(), attrMap);  
  223.                         } else {  
  224.                             map.put(iter.getName(), iter.getText());  
  225.                         }  
  226.                     }  
  227.                 }  
  228.             }  
  229.         } else {  
  230.             // 根節點的  
  231.             if (listAttr0.size() > 0) {  
  232.                 map.put("#text", element.getText());  
  233.             } else {  
  234.                 map.put(element.getName(), element.getText());  
  235.             }  
  236.         }  
  237.         return map;  
  238.     }  
  239.       
  240.     /** 
  241.      * map轉xml map中沒有根節點的鍵 
  242.      * @param map 
  243.      * @param rootName 
  244.      * @throws DocumentException 
  245.      * @throws IOException 
  246.      */  
  247.     public static Document map2xml(Map<String, Object> map, String rootName) throws DocumentException, IOException  {  
  248.         Document doc = DocumentHelper.createDocument();  
  249.         Element root = DocumentHelper.createElement(rootName);  
  250.         doc.add(root);  
  251.         map2xml(map, root);  
  252.         //System.out.println(doc.asXML());  
  253.         //System.out.println(formatXml(doc));  
  254.         return doc;  
  255.     }  
  256.       
  257.     /** 
  258.      * map轉xml map中含有根節點的鍵 
  259.      * @param map 
  260.      * @throws DocumentException 
  261.      * @throws IOException 
  262.      */  
  263.     public static Document map2xml(Map<String, Object> map) throws DocumentException, IOException  {  
  264.         Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();  
  265.         if(entries.hasNext()){ //獲取第一個鍵創建根節點  
  266.             Map.Entry<String, Object> entry = entries.next();  
  267.             Document doc = DocumentHelper.createDocument();  
  268.             Element root = DocumentHelper.createElement(entry.getKey());  
  269.             doc.add(root);  
  270.             map2xml((Map)entry.getValue(), root);  
  271.             //System.out.println(doc.asXML());  
  272.             //System.out.println(formatXml(doc));  
  273.             return doc;  
  274.         }  
  275.         return null;  
  276.     }  
  277.       
  278.     /** 
  279.      * map轉xml 
  280.      * @param map 
  281.      * @param body xml元素 
  282.      * @return 
  283.      */  
  284.     private static Element map2xml(Map<String, Object> map, Element body) {  
  285.         Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();  
  286.         while (entries.hasNext()) {  
  287.             Map.Entry<String, Object> entry = entries.next();  
  288.             String key = entry.getKey();  
  289.             Object value = entry.getValue();  
  290.             if(key.startsWith("@")){    //屬性  
  291.                 body.addAttribute(key.substring(1, key.length()), value.toString());  
  292.             } else if(key.equals("#text")){ //有屬性時的文本  
  293.                 body.setText(value.toString());  
  294.             } else {  
  295.                 if(value instanceof java.util.List ){  
  296.                     List list = (List)value;  
  297.                     Object obj;  
  298.                     for(int i=0; i<list.size(); i++){  
  299.                         obj = list.get(i);  
  300.                         //list裏是map或String,不會存在list裏直接是list的,  
  301.                         if(obj instanceof java.util.Map){  
  302.                             Element subElement = body.addElement(key);  
  303.                             map2xml((Map)list.get(i), subElement);  
  304.                         } else {  
  305.                             body.addElement(key).setText((String)list.get(i));  
  306.                         }  
  307.                     }  
  308.                 } else if(value instanceof java.util.Map ){  
  309.                     Element subElement = body.addElement(key);  
  310.                     map2xml((Map)value, subElement);  
  311.                 } else {  
  312.                     body.addElement(key).setText(value.toString());  
  313.                 }  
  314.             }  
  315.             //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
  316.         }  
  317.         return body;  
  318.     }  
  319.       
  320.     /** 
  321.      * 格式化輸出xml 
  322.      * @param xmlStr 
  323.      * @return 
  324.      * @throws DocumentException 
  325.      * @throws IOException 
  326.      */  
  327.     public static String formatXml(String xmlStr) throws DocumentException, IOException  {  
  328.         Document document = DocumentHelper.parseText(xmlStr);  
  329.         return formatXml(document);  
  330.     }  
  331.       
  332.     /** 
  333.      * 格式化輸出xml 
  334.      * @param document 
  335.      * @return 
  336.      * @throws DocumentException 
  337.      * @throws IOException 
  338.      */  
  339.     public static String formatXml(Document document) throws DocumentException, IOException  {  
  340.         // 格式化輸出格式  
  341.         OutputFormat format = OutputFormat.createPrettyPrint();  
  342.         //format.setEncoding("UTF-8");  
  343.         StringWriter writer = new StringWriter();  
  344.         // 格式化輸出流  
  345.         XMLWriter xmlWriter = new XMLWriter(writer, format);  
  346.         // 將document寫入到輸出流  
  347.         xmlWriter.write(document);  
  348.         xmlWriter.close();  
  349.         return writer.toString();  
  350.     }  
  351.   
  352. }  
package xml2map;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**

  • xml轉map,map轉xml 帶屬性

  • http://happyqing.iteye.com/blog/2316275

  • @author happyqing

  • @since 2016.8.8
    */
    @SuppressWarnings({ “rawtypes”, “unchecked” })
    public class XmlUtil {
    public static void main(String[] args) throws DocumentException, IOException {
    String textFromFile = FileUtils.readFileToString(new File(“D:/workspace/workspace_3.7/xml2map/src/xml2json/sample.xml”),“UTF-8”);
    Map<String, Object> map = xml2map(textFromFile, false);
    // long begin = System.currentTimeMillis();
    // for(int i=0; i<1000; i++){
    // map = (Map<String, Object>) xml2mapWithAttr(doc.getRootElement());
    // }
    // System.out.println(“耗時:”+(System.currentTimeMillis()-begin));
    JSON json = JSONObject.fromObject(map);
    System.out.println(json.toString(1)); // 格式化輸出

     Document doc = map2xml(map, "root");
     //Document doc = map2xml(map); //map中含有根節點的鍵
     System.out.println(formatXml(doc));
    

    }

    /**

    • xml轉map 不帶屬性
    • @param xmlStr
    • @param needRootKey 是否需要在返回的map里加根節點鍵
    • @return
    • @throws DocumentException
      */
      public static Map xml2map(String xmlStr, boolean needRootKey) throws DocumentException {
      Document doc = DocumentHelper.parseText(xmlStr);
      Element root = doc.getRootElement();
      Map<String, Object> map = (Map<String, Object>) xml2map(root);
      if(root.elements().size()==0 && root.attributes().size()==0){
      return map;
      }
      if(needRootKey){
      //在返回的map里加根節點鍵(如果需要)
      Map<String, Object> rootMap = new HashMap<String, Object>();
      rootMap.put(root.getName(), map);
      return rootMap;
      }
      return map;
      }

    /**

    • xml轉map 帶屬性
    • @param xmlStr
    • @param needRootKey 是否需要在返回的map里加根節點鍵
    • @return
    • @throws DocumentException
      */
      public static Map xml2mapWithAttr(String xmlStr, boolean needRootKey) throws DocumentException {
      Document doc = DocumentHelper.parseText(xmlStr);
      Element root = doc.getRootElement();
      Map<String, Object> map = (Map<String, Object>) xml2mapWithAttr(root);
      if(root.elements().size()==0 && root.attributes().size()==0){
      return map; //根節點只有一個文本內容
      }
      if(needRootKey){
      //在返回的map里加根節點鍵(如果需要)
      Map<String, Object> rootMap = new HashMap<String, Object>();
      rootMap.put(root.getName(), map);
      return rootMap;
      }
      return map;
      }

    /**

    • xml轉map 不帶屬性

    • @param e

    • @return
      */
      private static Map xml2map(Element e) {
      Map map = new LinkedHashMap();
      List list = e.elements();
      if (list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {
      Element iter = (Element) list.get(i);
      List mapList = new ArrayList();

       	if (iter.elements().size() &gt; 0) {
       		Map m = xml2map(iter);
       		if (map.get(iter.getName()) != null) {
       			Object obj = map.get(iter.getName());
       			if (!(obj instanceof List)) {
       				mapList = new ArrayList();
       				mapList.add(obj);
       				mapList.add(m);
       			}
       			if (obj instanceof List) {
       				mapList = (List) obj;
       				mapList.add(m);
       			}
       			map.put(iter.getName(), mapList);
       		} else
       			map.put(iter.getName(), m);
       	} else {
       		if (map.get(iter.getName()) != null) {
       			Object obj = map.get(iter.getName());
       			if (!(obj instanceof List)) {
       				mapList = new ArrayList();
       				mapList.add(obj);
       				mapList.add(iter.getText());
       			}
       			if (obj instanceof List) {
       				mapList = (List) obj;
       				mapList.add(iter.getText());
       			}
       			map.put(iter.getName(), mapList);
       		} else
       			map.put(iter.getName(), iter.getText());
       	}
       }
      

      } else
      map.put(e.getName(), e.getText());
      return map;
      }

    /**

    • xml轉map 帶屬性

    • @param e

    • @return
      */
      private static Map xml2mapWithAttr(Element element) {
      Map<String, Object> map = new LinkedHashMap<String, Object>();

      List<Element> list = element.elements();
      List<Attribute> listAttr0 = element.attributes(); // 當前節點的所有屬性的list
      for (Attribute attr : listAttr0) {
      map.put("@" + attr.getName(), attr.getValue());
      }
      if (list.size() > 0) {

       for (int i = 0; i &lt; list.size(); i++) {
       	Element iter = list.get(i);
       	List mapList = new ArrayList();
      
       	if (iter.elements().size() &gt; 0) {
       		Map m = xml2mapWithAttr(iter);
       		if (map.get(iter.getName()) != null) {
       			Object obj = map.get(iter.getName());
       			if (!(obj instanceof List)) {
       				mapList = new ArrayList();
       				mapList.add(obj);
       				mapList.add(m);
       			}
       			if (obj instanceof List) {
       				mapList = (List) obj;
       				mapList.add(m);
       			}
       			map.put(iter.getName(), mapList);
       		} else
       			map.put(iter.getName(), m);
       	} else {
      
       		List&lt;Attribute&gt; listAttr = iter.attributes(); // 當前節點的所有屬性的list
       		Map&lt;String, Object&gt; attrMap = null;
       		boolean hasAttributes = false;
       		if (listAttr.size() &gt; 0) {
       			hasAttributes = true;
       			attrMap = new LinkedHashMap&lt;String, Object&gt;();
       			for (Attribute attr : listAttr) {
       				attrMap.put("@" + attr.getName(), attr.getValue());
       			}
       		}
      
       		if (map.get(iter.getName()) != null) {
       			Object obj = map.get(iter.getName());
       			if (!(obj instanceof List)) {
       				mapList = new ArrayList();
       				mapList.add(obj);
       				// mapList.add(iter.getText());
       				if (hasAttributes) {
       					attrMap.put("#text", iter.getText());
       					mapList.add(attrMap);
       				} else {
       					mapList.add(iter.getText());
       				}
       			}
       			if (obj instanceof List) {
       				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());
      }
      }
      return map;
      }

    /**

    • map轉xml map中沒有根節點的鍵
    • @param map
    • @param rootName
    • @throws DocumentException
    • @throws IOException
      */
      public static Document map2xml(Map<String, Object> map, String rootName) throws DocumentException, 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));
      return doc;
      }

    /**

    • map轉xml map中含有根節點的鍵
    • @param map
    • @throws DocumentException
    • @throws IOException
      */
      public static Document map2xml(Map<String, Object> map) throws DocumentException, IOException {
      Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
      if(entries.hasNext()){ //獲取第一個鍵創建根節點
      Map.Entry<String, Object> 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));
      return doc;
      }
      return null;
      }

    /**

    • map轉xml
    • @param map
    • @param body xml元素
    • @return
      */
      private static Element map2xml(Map<String, Object> map, Element body) {
      Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
      while (entries.hasNext()) {
      Map.Entry<String, Object> 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 instanceof java.util.List ){
      List list = (List)value;
      Object obj;
      for(int i=0; i<list.size(); i++){
      obj = list.get(i);
      //list裏是map或String,不會存在list裏直接是list的,
      if(obj instanceof java.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 instanceof java.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());
      }
      return body;
      }

    /**

    • 格式化輸出xml
    • @param xmlStr
    • @return
    • @throws DocumentException
    • @throws IOException
      */
      public static String formatXml(String xmlStr) throws DocumentException, IOException {
      Document document = DocumentHelper.parseText(xmlStr);
      return formatXml(document);
      }

    /**

    • 格式化輸出xml
    • @param document
    • @return
    • @throws DocumentException
    • @throws IOException
      */
      public static String formatXml(Document document) throws DocumentException, IOException {
      // 格式化輸出格式
      OutputFormat format = OutputFormat.createPrettyPrint();
      //format.setEncoding(“UTF-8”);
      StringWriter writer = new StringWriter();
      // 格式化輸出流
      XMLWriter xmlWriter = new XMLWriter(writer, format);
      // 將document寫入到輸出流
      xmlWriter.write(document);
      xmlWriter.close();
      return writer.toString();
      }

}

 

參考:

Java xml文件轉換爲map集合

http://my.oschina.net/u/2253208/blog/500877
java xml轉map 高級版
http://xuelianbobo.iteye.com/blog/2153384

 

資深Java項目團隊歷時1年打造,實戰精髓大揭祕!
Java系統學習全案:五大階段學習系統規劃、8大企業級項目貫穿全程。限時2折秒殺。立省4688元!
<div id="share_weibo">分享到:
  <a data-type="sina" href="javascript:;" title="分享到新浪微博"><img src="/images/sina.jpg"></a>
  <a data-type="qq" href="javascript:;" title="分享到騰訊微博"><img src="/images/tec.jpg"></a>
</div>
  <li>分類:<a href="https://www.iteye.com/blogs/category/language">編程語言</a></li>      
  <li class="last"><a href="https://www.iteye.com/wiki/blog/2316275" target="_blank" class="more">查看更多</a></li>
</ul>    
評論
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章