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;
        }
    
    }
    

      

  • 相关阅读:
    __weak&__block&__unsafe__unretain
    Unable to convert MySQL date/time value to System.DateTime 错误
    做最好的自己,人生十件事(事业,人生,情感)
    52张扑克牌的Suit(花色)和Rank(牌面大小)排序算法
    windows service在服务器上部署时的问题
    "Unable to compile template. Check the Errors list for details" 问题解决办法
    Javascript中的一些自有方法
    winform中的webbrowser里面操作html代码问题
    Javascript,Jquery实现页面图片预加载百分比展现
    【delphi XE 】 Margin属性 转
  • 原文地址:https://www.cnblogs.com/l412382979/p/9014252.html
Copyright © 2011-2022 走看看