java xml轉map map轉xml 帶屬性
阿新 • • 發佈:2019-02-17
參考效果:xml轉json
java xml轉map
Java程式碼- 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
- 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() > 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 < list.size(); i++) {
- Element iter = list.get(i);
- List mapList = new ArrayList();
- if (iter.elements().size() > 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<Attribute> listAttr = iter.attributes(); // 當前節點的所有屬性的list
- Map<String, Object> attrMap = null;
- boolean hasAttributes = false;
- if (listAttr.size() > 0) {
- hasAttributes = true;
- attrMap = new LinkedHashMap<String, Object>();
- 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