zoukankan      html  css  js  c++  java
  • JAVA生成XML文件

     Java Bean:

    package xml;
    
    public class Person {
    private String name ;
    private int age ;
    
    
    public Person(String name, int age) {
      super();
      this.name = name;
      this.age = age;
    }
    
    public String getName() {
      return name;
    }
    
    public void setName(String name) {
      this.name = name;
    }
    
    public int getAge() {
      return age;
    }
    
    public void setAge(int age) {
      this.age = age;
    }
    
    
    }
    
    详细实现:

    package xml;
    
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    
    public class XmlGeneratorDemo {
    public static void main(String[] args) {
      String outputPath = System.getProperty("user.dir")+File.separator + "src"+File.separator+"person.xml";
      generateXml(outputPath);
    }
    public static void generateXml(String outputPath) {
      try {
       Person [] arr = new Person[]{new Person("jinbo",20), new Person("gameboy",25)};
       List<Person> list = Arrays.asList(arr);//将数组转换成List
       Document doc = generateXml(list);//生成XML文件
       outputXml(doc, outputPath);//将文件输出到指定的路径
      } catch (Exception e) {
       System.err.println("出现异常");
      }
    }
    /**
      * 将XML文件输出到指定的路径
      * @param doc
      * @param fileName
      * @throws Exception
      */
    private static void outputXml (Document doc, String fileName) throws Exception{
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = tf.newTransformer();
      DOMSource source = new DOMSource(doc);
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");//设置文档的换行与缩进
      PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
      StreamResult result = new StreamResult(pw);
      transformer.transform(source, result);
      System.out.println("生成XML文件成功!");
    }
    /**
      * 生成XML文件
      * @param list
      * @return
      */
    public static Document generateXml(List<Person> list){
      Document doc = null;
      Element root = null;
      try {
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();
       doc = builder.newDocument();
       root = doc.createElement("person");
       doc.appendChild(root);
      } catch (Exception e) {
       e.printStackTrace();
       return null;//假设出现异常,则不再往下运行
      }
      
      int len = list.size() ;
      Element element ;
      for (int i = 0; i < len; i++) {
       Person person = list.get(i);
       element = doc.createElement("person"+(i+1));
       element.setAttribute("age", ""+person.getAge());
       element.setAttribute("name", person.getName());
       root.appendChild(element);
      }
      return doc;
    }
    }


  • 相关阅读:
    确保消息产生前数据库操作已提交
    信息披露和公司简介总结
    1、清空所有,给当前添加/2、清空上一个,给当前添加。
    不能作为判断条件的:
    excel表格 函数功能
    一种ui app写法
    正则中使用变量及数组去重的方法
    鼠标锁定(消失),进入无限滚动状态
    transform 的旋转 ,3d效果,要添加3d效果的父级加上景深perspective。 3d效果的容器加上 transform-style:preserve-3d。
    rem布局,在用户调整手机字体大小/用户调整浏览器字体大小后,布局错乱问题
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5126800.html
Copyright © 2011-2022 走看看