jaxb能够很好的读写xml,主要用于java对象和xml文件的互转,以下是一个java对象转成xml文件的例子
首先是bean文件,如下:
写入xml文件中
对日期格式进行特殊的处理,需要继承xmladapter,从date类型转成string类型
生成如下xml文件:
首先是bean文件,如下:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestBean implements Serializable {
@XmlTransient
private static final long serialVersionUID = 1L;
private String name;
private float price;
private int age;
private String add;
/* list的表现形式
* <lists>
* <list></list>
* <list></list>
* </lists>
* */
@XmlElementWrapper(name="DEGREES")
@XmlElements({//表示或的关系,list中内容可以为以下两种类型
@XmlElement(name="Degree",type=Degrees.class),
@XmlElement(name="b",type=String.class)
})
private List<Degrees> degrees;
@XmlJavaTypeAdapter(value=JaxbAdapter.class)
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public List<Degrees> getDegrees() {
return degrees;
}
public void setDegrees(List<Degrees> degrees) {
this.degrees = degrees;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder str = new StringBuilder();
str.append("[name: " + name);
str.append("-- price: " + price);
str.append("-- age: " + age);
str.append("-- add: " + add + "]");
return super.toString();
}
}
写入xml文件中
public class JaxbWriteXml {
public static void main(String args[]) throws JAXBException
{
JAXBContext context = JAXBContext.newInstance(TestBean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
TestBean bean = createBean();
// TestBean2 bean2 = createBean2();
marshaller.marshal(bean , new File("test.xml"));
}
private static TestBean2 createBean2() {
// TODO Auto-generated method stub
TestBean2 bean2 = new TestBean2();
bean2.setId(02);
bean2.setName("你好!");
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
bean2.setList(list);
return bean2;
}
private static TestBean createBean() {
// TODO Auto-generated method stub
TestBean bean = new TestBean();
bean.setName("张三丰");
bean.setAge(23);
bean.setPrice(new Float(29.7));
bean.setAdd("wenzu road NO.6 nanjing china");
bean.setDate(new Date());
bean.setDegrees(getDegreess());
return bean;
}
private static List<Degrees> getDegreess() {
// TODO Auto-generated method stub
List<Degrees> degrees = new ArrayList<Degrees>();
Degrees degree = new Degrees();
degree.setdID("1");
degree.setdName("大学");
degrees.add(degree);
degree = new Degrees();
degree.setdID("2");
degree.setdName("高中");
degrees.add(degree);
degree = new Degrees();
degree.setdID("3");
degree.setdName("博士");
degrees.add(degree);
return degrees;
}
}
对日期格式进行特殊的处理,需要继承xmladapter,从date类型转成string类型
public class JaxbAdapter extends XmlAdapter<String, Date> {
@Override
public Date unmarshal(String v) throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public String marshal(Date v) throws Exception {
// TODO Auto-generated method stub
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd");
return simple.format(v);
}
}
生成如下xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testBean>
<name>张三丰</name>
<price>29.7</price>
<age>23</age>
<add>wenzu road NO.6 nanjing china</add>
<DEGREES>
<Degree>
<dID>1</dID>
<dName>大学</dName>
</Degree>
<Degree>
<dID>2</dID>
<dName>高中</dName>
</Degree>
<Degree>
<dID>3</dID>
<dName>博士</dName>
</Degree>
</DEGREES>
<date>2012/05/09</date>
</testBean>