zoukankan      html  css  js  c++  java
  • Marshaller根据对象生成xml文件

    创建Girl.java类

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "root")
    public class Girl {
        @XmlAttribute(name = "type")
        private String type;
        @XmlElement(name = "name")
        private String name;
    
        @XmlElement(name = "age")
        private String age;
        
        @XmlElement(name = "girl")
        private List<Girl> girlList;
    
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (Girl girl : girlList) {
                sb.append(girl.toString());
            }
            return sb.toString();
        }
        
        public Girl(){  
            super();  
        }  
          
        public Girl(String name,String age){  
            this.name=name;  
            this.age=age;  
        } 
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Girl> getGirlList() {
            return girlList;
        }
    
        public void setGirlList(List<Girl> girlList) {
            this.girlList = girlList;
        }
        
    }
    

      编写测试类

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class TestUnm {
        public static void main(String[] args) {
            marshaller();
        }
    
        public static void marshaller() {
            try {
                JAXBContext jaxbC = JAXBContext.newInstance(Girl.class);
                Marshaller ms = jaxbC.createMarshaller();
                ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                Girl girl=new Girl();
                List<Girl> girls=new ArrayList<Girl>();  
                Girl g1=new Girl("小红", "20");  
                Girl g2=new Girl("小芳", "16");  
                Girl g3=new Girl("小丽", "17"); 
                girls.add(g1);
                girls.add(g2);
                girls.add(g3);
                girl.setGirlList(girls);
                StringWriter sw=new StringWriter();  
                ms.marshal(girl, sw);  
                System.out.println(sw.toString());
                createFile("E:\app\","test.xml",sw.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static boolean createFile(String path,String fileName,String fileContent){
            boolean bool=false;
            File file=new File(path+File.separator+fileName);
            try {
                if(!file.exists()){
                    file.createNewFile();
                    bool=true;
                }
                writeFileContent(path+File.separator+fileName,fileContent);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bool;
        }
        
        public static boolean writeFileContent(String path,String fileContent){
            boolean bool=false;
            FileOutputStream fos=null;
            PrintWriter pw=null;
            try {
                File file=new File(path);
                fos=new FileOutputStream(file);
                OutputStreamWriter  writer=new OutputStreamWriter(fos, "UTF-8");
                pw=new PrintWriter(writer);
                pw.write(fileContent);
                pw.flush();
                bool=true;
            } catch (Exception e) {
               e.printStackTrace();
            }
            return bool;
        }
    
    }
    

      

  • 相关阅读:
    [ZOJ 4062][2018ICPC青岛站][Plants vs. Zombies]
    [Wannafly挑战赛28][B msc和mcc][预处理+枚举]
    [codeforces Mail.Ru Cup 2018 Round 1 D][ xor 操作]
    [codeforces round#475 div2 ][C Alternating Sum ]
    [zoj4045][思维+dfs]
    [zoj4046][树状数组求逆序(强化版)]
    费马大定理以及求解a^2+b^2=c^2的奇偶数列法则
    【HDOJ3567】【预处理bfs+映射+康拓展开hash】
    POJ1279 Art Gallery 多边形的核
    第八周 Leetcode 44. Wildcard Matching 水题 (HARD)
  • 原文地址:https://www.cnblogs.com/l412382979/p/9014252.html
Copyright © 2011-2022 走看看