JAVA生成xml 文件代码

阅读更多

直接贴代码

 

 

package org.bean.pub;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;  
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import bean.Fxspd;
public class GenerateXML {
 public static  String path="\\\\Szserver\\PriceSystem\\";
 public static  void  shengchengXML(List speObjs,String name,String txname) { 
  XMLWriter writer = null;// 声明写XML的对象 
        OutputFormat format = OutputFormat.createPrettyPrint(); 
        format.setEncoding("UTF-8");// 设置XML文件的编码格式 
        String filePath = path+txname+"\\"; 
        File file = new File(filePath); 
        if(!file.exists()) {
   file.mkdirs();
  }
        Document _document = DocumentHelper.createDocument(); 
        Element _root = _document.addElement("PriceSystem"); 
        PriceSystem ps;
  for (PriceSystem objects : speObjs) {
   ps=objects;
      Element cp =    _root.addElement("CP").addAttribute("ID", String.valueOf(ps.getCpid())); 
      cp.addElement("CPLB").addAttribute("ID", String.valueOf(ps.getCplbid())).addText(ps!=null?(ps.getCplb()):"");
      cp.addElement("CPMC").addText(ps.getCpmc()); 
      cp.addElement("GG").addAttribute("ID",String.valueOf(ps.getGgid())).addText(ps.getGg());
      cp.addElement("DW").addAttribute("ID", String.valueOf(ps.getDwid())).addText(ps.getDw());
      cp.addElement("JG").addText(ps.getJg()!=null?ps.getJg():"0.00");
      cp.addElement("CPJC").addText(ps.getCpjc());
    
  }
        try {
    writer = new XMLWriter(new FileWriter(filePath+name), format);
  } catch (IOException e) {
   // TODO 自动生成的 catch 块
   e.printStackTrace();
  } 
        try {
   writer.write(_document);
   writer.close(); 
  } catch (IOException e) {
   // TODO 自动生成的 catch 块
   e.printStackTrace();
  } 
       
 }
 public  static void  delXml(String pth){
  File file =new File(pth);
  if(file.exists()){
   file.delete();
  }
 }
 
 

}

 

 

生成的xml

 


 
    干货
    新西兰黄油
   
   
    5.00
    xxlhy
 

 
    速冻类
    西域肉串
    25串/包
   
    4.00
    xyrc
 

 
    干货
    西洋参
   
   
    3.00
    xyc,s,c
 

 
    牛肉及其他肉类
    西式B级牛柳
   
   
    2.00
    xsBjnl
 

 
    蔬菜类
    西芹
   
   
    1.00
    xq
 

你可能感兴趣的:(JAVA生成xml 文件代码)