代码改变世界

Marshaller根据对象生成xml文件

2018-05-09 15:00  不痒不痛  阅读(1942)  评论(0编辑  收藏  举报

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

}