dom4j生成xml方法

如果想要使用xml来传递数据,不管是网络传递,还是数据库之间的传递,dom4j都是一个好用的工具。

直接举个例子说明数据转化为xml文件。

import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import java.io.FileOutputStream; import java.io.File; import java.io.FileNotFoundException; public class Dom4jXmlOper { private static String filePath = ""; public static String getFilePath() { return filePath; } public static void setFilePath(String filePath) { Dom4jXmlOper.filePath = filePath; } @SuppressWarnings("unchecked") public static void DB2Xml(Map<String, List<Map>> map) { Document _document = DocumentHelper.createDocument();//创建document对象, Element _root = _document.addElement("root"); Iterator ite = map.entrySet().iterator(); while (ite.hasNext()) { Map.Entry tableEntry = (Entry) ite.next(); String tableKey = (String) tableEntry.getKey(); List<Map> dataStructList = map.get(tableKey); Element _table = _root.addElement(tableKey); for (int i = 0; i < dataStructList.size(); i++) { Map tempMap = dataStructList.get(i); Iterator it = tempMap.entrySet().iterator(); Element _record = _table.addElement("record"); while (it.hasNext()) { Map.Entry entry = (Entry) it.next(); String key = (String) entry.getKey(); if ("id".equalsIgnoreCase(key)) { continue; } Object value = entry.getValue(); _record.addAttribute(key, value.toString()); } } } OutputXml(_document); } /* * doc 包括内容的document对象 * 作用:将doc输出为 xml文件 * filePath 为String类型的路径+文件名 */ public static void OutputXml(Document doc) { XMLWriter writer = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// 设置XML文件的编码格式,如果有中文可设置为GBK或UTF-8 File file = new File(filePath); //如果读取的内容中没有中文,可以使用以下的几行代码生成xml // // try { // writer = new XMLWriter(new FileWriter(file), format); // } catch (IOException e1) { // e1.printStackTrace(); // } // 如果上面设置的xml编码类型为GBK,或设为UTF-8但其中有中文则应当用FileWriter来构建xml文件(使用以下代码),否则会出现中文乱码问题 FileOutputStream fos = null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { writer = new XMLWriter(fos, format); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { writer.write(doc); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }

说明:

@SuppressWarnings("unchecked")
 public static void DB2Xml(Map<String, List<Map>> map) {

Document _document = DocumentHelper.createDocument();//创建document对象
  Element _root = _document.addElement("root");  //创建根据节点

Iterator ite = map.entrySet().iterator(); //遍历map
  while (ite.hasNext()) {
   Map.Entry tableEntry = (Entry) ite.next();
   String tableKey = (String) tableEntry.getKey();//获得数据表名
   List<Map> dataStructList = map.get(tableKey);
   Element _table = _root.addElement(tableKey);//每一个数据表作为根节点的一个子节点
   for (int i = 0; i < dataStructList.size(); i++) {//遍历数据表中的记录
    Map tempMap = dataStructList.get(i);
    Iterator it = tempMap.entrySet().iterator();
    Element _record = _table.addElement("record");//每一条记录做一个数据表的子节点
    while (it.hasNext()) {
     Map.Entry entry = (Entry) it.next();
     String key = (String) entry.getKey();
        Object value = entry.getValue();
     _record.addAttribute(key, value.toString());//每一个字段值对,做一个记录节点的属性
    }
   }
  }
  OutputXml(_document);
 }

/*
  * doc 包括内容的document对象
  * 作用:将doc输出为 xml文件
  * filePath 为String类型的路径+文件名
  */
 public static void OutputXml(Document doc) {
  XMLWriter writer = null;
  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding("UTF-8");// 设置XML文件的编码格式,如果有中文可设置为GBK或UTF-8
  File file = new File(filePath);
  //如果读取的内容中没有中文,可以使用以下的几行代码生成xml
//
//  try {
//   writer = new XMLWriter(new FileWriter(file), format);
//  } catch (IOException e1) {
//   e1.printStackTrace();
//  }

  // 如果上面设置的xml编码类型为GBK,或设为UTF-8但其中有中文则应当用FileWriter来构建xml文件(使用以下代码),否则会出现中文乱码问题
   FileOutputStream fos = null;
   try {
   fos = new FileOutputStream(file);
   } catch (FileNotFoundException e) {
   e.printStackTrace();
   }
   try {
   writer = new XMLWriter(fos, format);
   } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
   }
  try {
   writer.write(doc);
   writer.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}


   使用红色字体生成的xml文件,如果使用写字板打开,中文会出现乱码,因为写字板默认字符集为ansi。使用浏览器或记录本打开均为正确中文。

  使用绿色字体生成的xml文件,则写字板为正确,其他为乱码。

你可能感兴趣的:(xml,String,object,File,null,iterator)