使用jaxb 轉換bo物件及XML檔案流
阿新 • • 發佈:2018-12-26
bo物件:
注:
jaxb只會序列化 public 的方法 ,若屬性宣告為public,且宣告 @XmlElement(name = "name") ,則需要在該屬性get方法上宣告@XmlTransient,表示不將get方法序列化成xml元素,否則會報重複屬性序列化。或者將屬性改為private ,將@XmlElement(name = "name")放置在get方法上。
其二:若尋在 屬性中有 Date型別,需要繼承XmlAdapter<String, Date>類,並重載2個方法後,在bo物件處使用@XmlJavaTypeAdapter(JaxbDateSerializer.class) 進行序列化。
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 = "child") 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; } }
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.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{
//java 類 轉 xml 檔案
List<Child> l = new ArrayList<Child>();
Person p = new Person();
p.setAge("123");
p.setName("zhoushun");
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();
JAXBContext context = JAXBContext.newInstance(Person.class,Child.class);
Marshaller m = context.createMarshaller();
m.marshal(p, writer);
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("person.xml");
System.out.println(xmlFileUrl.getPath());
Person p2 = (Person) unmarshaller.unmarshal(xmlFileUrl);
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>zhoushun123</name><age>123</age><child><sex>0</sex></child><child><sex>1</sex></child><child><sex>2</sex></child><child><sex>3</sex></child><child><sex>4</sex></child><d>2012-06-01</d>
</person>
<person><name>zhoushun125</name><age>123</age><child><sex>0</sex></child><child><sex>1</sex></child><child><sex>2</sex></child><child><sex>3</sex></child><child><sex>4</sex></child><d>2012-06-01</d>
</person>