EclipseLink MOXy的JAXB实现可以定制生成的XML文件。
一 如何使用 EclipseLink的 MOXy的JAXB特性
1) 引入 org.eclipse.persistence.moxy_2.3.1.v20110908-r10021.jar,org.eclipse.persistence.core.jar或者eclipselink.jar
2) 创建jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
将其放在模型对象的同级目录包之下。
二 简单案例
moxy.pojo.Animal.java
package moxy.pojo; import javax.xml.bind.annotation.XmlTransient; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlTransient public class Animal { @XmlPath("xml/id/text()") private String id; @XmlPath("xml/name/text()") private String name; @XmlPath("xml/desc/text()") private String desc; public Animal() {} public Animal(String id, String name, String desc) { this.id = id; this.name = name; this.desc = desc; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return this.id+":"+this.name+":"+this.desc; } }
moxy.pojo.Dog.java
package moxy.pojo; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; //@XmlTransient @XmlRootElement @XmlAccessorType public class Dog extends Animal { private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
moxy.pojo.package.java
@XmlSchema(namespace="this is the default namespace",elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package moxy.pojo; import javax.xml.bind.annotation.XmlSchema;
moxy/pojo/jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
util.JAXBUtil.java
package util; import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.io.Writer; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JAXBUtil { public static String marshalData(Object object) throws Exception { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");// 编码格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 // 是否省略xml头信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>) marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); Writer writer =new StringWriter(); marshaller.marshal(object,writer); writer.close(); return writer.toString(); } // 解组数据 @SuppressWarnings("unchecked") public static <T> T unmarshalData(String xml,Class<T> clazz) throws Exception { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); T object = (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes())); return object; } }
测试类 moxy.test.Main.java
package moxy.test; import moxy.pojo.Dog; import moxy.pojo.Animal; import util.JAXBUtil; public class Main { public static void main(String[] args) throws Exception { testDog(); } public static void testDog()throws Exception{ System.out.println("marshal data"); Dog dog = new Dog(); dog.setId("001"); dog.setName("dog"); dog.setDesc("lovely dog"); dog.setColor("yellow"); String xml = JAXBUtil.marshalData(dog); System.out.println(xml); System.out.println("unmarshal data"); Dog object = JAXBUtil.unmarshalData(xml, Dog.class); System.out.println(object); } }
生成xml文件如下
<dog xmlns="this is the default namespace"> <xml> <id>001</id> <name>dog</name> <desc>lovely dog</desc> </xml> <color>yellow</color> </dog>
Note:
1) 要使用MOXy特性必须添加jaxb.properties并发在要转化的模型对象的同级目录
2) package-info.java 可以指定同级目录的模型对象转化成xml文件的namespace信息
3) 可以通过XmlPath 指定最终生成的xml文件格式
4) 如果有继承,最终生成的xml可能会有xsi信息,可以通过添加@XmlTransient消除这部分信息