1. 程式人生 > >Jaxb實現 複雜POJO 與 xml 的轉換

Jaxb實現 複雜POJO 與 xml 的轉換

Person:

package com.test;

import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "person")
public class Person {
	@XmlElement(name = "name")
	private String name ;
	
	@XmlElement(name = "age")
	private String age;
	
	@XmlElement(name = "BasicData")
	private List<Child> children;
	
	
	private Date d;
	
	@XmlElement(name = "time")
	@XmlJavaTypeAdapter(JaxbDateSerializer.class) 
	public Date getD() {
		return d;
	}
	public void setD(Date d) {
		this.d = d;
	}
	
	@XmlTransient
	public List<Child> getChildren() {
		return children;
	}
	public void setChildren(List<Child> children) {
		this.children = children;
	}
	@XmlTransient
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@XmlTransient
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
}


Child.java

package com.test;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement(name="BasicData")
@XmlAccessorType(XmlAccessType.FIELD)
public class Child {
	private String sex;

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
}


JaxbDateSerializer.java 處理Date

package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JaxbDateSerializer extends XmlAdapter<String, Date>{
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override 
    public String marshal(Date date) throws Exception { 
        return dateFormat.format(date); 
    }
    @Override 
    public Date unmarshal(String date) throws Exception { 
        return dateFormat.parse(date); 
    } 
}


Main

package com.test;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


public class Test {
	public static void main(String[] args) throws JAXBException, IOException{
		System.out.println(java.net.URLEncoder.encode("的","utf-8"));
		//java 類 轉 xml 檔案
		List<Child> l = new ArrayList<Child>();
		Person p = new Person();
		p.setAge("123");
		p.setName("周順");
		p.setD(new Date());
		for(int i=0;i<5;i++){
			Child c = new Child();
			c.setSex(i+"");
			l.add(c);
		}
		p.setChildren(l);
		StringWriter writer = new StringWriter();
		URL read = ClassLoader.getSystemResource("com/test/person.xml");
		FileWriter fw = new FileWriter(read.getPath());
		JAXBContext context = JAXBContext.newInstance(Person.class);
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
		m.setProperty(Marshaller.JAXB_ENCODING, "GBK"); //防止檔案中文亂碼
		m.marshal(p, writer);
		m.marshal(p, fw);
		fw.flush();
		fw.close();
		String xml = writer.toString();
		System.out.println(xml);
		
		
	//	xml檔案轉java類
		 JAXBContext context2 = JAXBContext.newInstance(Person.class);
	        Unmarshaller unmarshaller = context2.createUnmarshaller();
	        //class目錄 jaxb/bin
	        //URL xmlFileUrl = ClassLoader.getSystemResource("");
	        URL xmlFileUrl = ClassLoader.getSystemResource("com/test/person.xml");
	        System.out.println(xmlFileUrl.getPath());
	        Person p2 = (Person) unmarshaller.unmarshal(xmlFileUrl);
               //  SAXReader reader = new SAXReader();   
          //  Document document = reader.read(new File(
           //Test.class.getClassLoader().getResource("com/test/person.xml").getPath()));
       // Person p2 = (Person) unmarshaller.unmarshal(new StringReader(document.asXML()));
	        System.out.println(p2.getName());
	        System.out.println(p2.getChildren().get(4).getSex());
	}
}


xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person><name>zhoushun</name><age>123</age><BasicData><sex>0</sex></BasicData><BasicData><sex>1</sex></BasicData><BasicData><sex>2</sex></BasicData><BasicData><sex>3</sex></BasicData><BasicData><sex>4</sex></BasicData><time>2013-10-31</time>
</person>