dom4j生成xml方法

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

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

[c-sharp]  view plain copy
  1. import java.io.IOException;  
  2.   
  3. import org.dom4j.Document;  
  4. import org.dom4j.DocumentHelper;  
  5. import org.dom4j.Element;  
  6. import org.dom4j.io.OutputFormat;  
  7. import org.dom4j.io.XMLWriter;  
  8.   
  9. import java.io.FileOutputStream;  
  10.   
  11. import java.io.File;  
  12. import java.io.FileNotFoundException;  
  13.   
  14.    
  15.   
  16. public class Dom4jXmlOper {  
  17.   
  18.    
  19.   
  20. private static String filePath = "";  
  21.   
  22.  public static String getFilePath() {  
  23.   return filePath;  
  24.  }  
  25.   
  26.  public static void setFilePath(String filePath) {  
  27.   Dom4jXmlOper.filePath = filePath;  
  28.  }  
  29.   
  30. @SuppressWarnings("unchecked")  
  31.  public static void DB2Xml(Map<String, List<Map>> map) {  
  32.   
  33. Document _document = DocumentHelper.createDocument();//创建document对象,  
  34.   Element _root = _document.addElement("root");   
  35.   
  36. Iterator ite = map.entrySet().iterator();  
  37.   while (ite.hasNext()) {  
  38.    Map.Entry tableEntry = (Entry) ite.next();  
  39.    String tableKey = (String) tableEntry.getKey();  
  40.    List<Map> dataStructList = map.get(tableKey);  
  41.    Element _table = _root.addElement(tableKey);  
  42.    for (int i = 0; i < dataStructList.size(); i++) {  
  43.     Map tempMap = dataStructList.get(i);  
  44.     Iterator it = tempMap.entrySet().iterator();  
  45.     Element _record = _table.addElement("record");  
  46.     while (it.hasNext()) {  
  47.      Map.Entry entry = (Entry) it.next();  
  48.      String key = (String) entry.getKey();  
  49.      if ("id".equalsIgnoreCase(key)) {  
  50.       continue;  
  51.      }  
  52.      Object value = entry.getValue();  
  53.      _record.addAttribute(key, value.toString());  
  54.     }  
  55.    }  
  56.   }  
  57.   OutputXml(_document);  
  58.  }  
  59.   
  60. /* 
  61.   * doc 包括内容的document对象  
  62.   * 作用:将doc输出为 xml文件 
  63.   * filePath 为String类型的路径+文件名 
  64.   */  
  65.  public static void OutputXml(Document doc) {  
  66.   XMLWriter writer = null;  
  67.   OutputFormat format = OutputFormat.createPrettyPrint();  
  68.   format.setEncoding("UTF-8");// 设置XML文件的编码格式,如果有中文可设置为GBK或UTF-8  
  69.   File file = new File(filePath);  
  70.   //如果读取的内容中没有中文,可以使用以下的几行代码生成xml  
  71. //  
  72. //  try {  
  73. //   writer = new XMLWriter(new FileWriter(file), format);  
  74. //  } catch (IOException e1) {  
  75. //   e1.printStackTrace();  
  76. //  }  
  77.   
  78.   // 如果上面设置的xml编码类型为GBK,或设为UTF-8但其中有中文则应当用FileWriter来构建xml文件(使用以下代码),否则会出现中文乱码问题  
  79.    FileOutputStream fos = null;  
  80.    try {  
  81.    fos = new FileOutputStream(file);  
  82.    } catch (FileNotFoundException e) {  
  83.    e.printStackTrace();  
  84.    }  
  85.    try {  
  86.    writer = new XMLWriter(fos, format);  
  87.    } catch (UnsupportedEncodingException e) {  
  88.    e.printStackTrace();  
  89.    }  
  90.   try {  
  91.    writer.write(doc);  
  92.    writer.close();  
  93.   } catch (IOException e) {  
  94.    e.printStackTrace();  
  95.   }  
  96.  }  
  97. }  

说明:

@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文件,则写字板为正确,其他为乱码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值