JAXB的XML和Object的自动转换
JAXB可以实现XML和Object的自动转换,这样我们就不用再自己写xml的解析了。
1、从xml文件读取自动转换成Object
2、从xml字符串读取自动转换成Object
3、把对象转化成xml输出到控制台
4、把对象转化成xml输出到文件
5、把对象转化成xml字符串
vdi.xml
<?xml version="1.0" encoding="UTF-8"?> <vdis size="2" batch_number="20141212123456" errmsg=""> <vdi> <id>1</id> <uuid>6524552158933218</uuid> <name>disk1</name> <disk_size>20</disk_size> </vdi> <vdi> <id>2</id> <uuid>6524552158933123</uuid> <name>disk2</name> <disk_size>40</disk_size> </vdi> </vdis>
Vdis.java
package com.chinac.jaxb.parsexml; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "vdis") @XmlAccessorType(XmlAccessType.FIELD) public class Vdis extends ArrayList<Vdi>{ @XmlAttribute(name = "size") private int size; @XmlAttribute(name = "batch_number") private Long batchNumber; @XmlAttribute(name = "errmsg") private String errmsg; @XmlElement(name = "vdi") private List<Vdi> getVdis(){ return this; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public Long getBatchNumber() { return batchNumber; } public void setBatchNumber(Long batchNumber) { this.batchNumber = batchNumber; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } @Override public String toString() { return "size:"+size + ",batchNumber:"+batchNumber + ",errmsg:"+errmsg + ""; } }
Vdi.java
package com.chinac.jaxb.parsexml; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @XmlAccessorType(XmlAccessType.FIELD) public class Vdi { @XmlElement(name = "id") private int id; @XmlElement(name = "uuid") private String uuid; @XmlElement(name = "name") private String name; @XmlElement(name = "disk_size") private int diskSize; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDiskSize() { return diskSize; } public void setDiskSize(int diskSize) { this.diskSize = diskSize; } @Override public String toString() { return "id:"+id + ",uuid:"+uuid + ",name:"+name + ",diskSize:"+diskSize; } }
Test.java
package com.chinac.jaxb.test; import java.io.File; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; import com.chinac.jaxb.parsexml.Vdi; import com.chinac.jaxb.parsexml.Vdis; @SuppressWarnings("unchecked") public class Test { //从xml文件读取 public static <T> T readFromFile(Class<T> clazz , String filePath) throws JAXBException{ JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jc.createUnmarshaller(); return (T) unmarshaller.unmarshal(new File(filePath)); } //从xml字符串读取 public static <T> T readFromString(Class<T> clazz , StringBuffer xmlStr) throws JAXBException{ JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jc.createUnmarshaller(); return (T) unmarshaller.unmarshal(new StreamSource(new StringReader(xmlStr.toString()))); } //把对象转化成xml输出到控制台 public static <T> void writeToConsole(Class<T> clazz , T t) throws JAXBException{ JAXBContext cxt = JAXBContext.newInstance(clazz); Marshaller marshaller = cxt.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 默认false表示xml指令存在 marshaller.marshal(t, System.out); } //把对象转化成xml输出到文件 public static <T> void writeToFile(Class<T> clazz , T t , String filePath) throws JAXBException{ JAXBContext cxt = JAXBContext.newInstance(clazz); Marshaller marshaller = cxt.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 默认false表示xml指令存在 marshaller.marshal(t, new File(filePath)); } //把对象转化成xml字符串 public static <T> String writeToString(Class<T> clazz , T t) throws JAXBException{ JAXBContext cxt = JAXBContext.newInstance(clazz); Marshaller marshaller = cxt.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 默认false表示xml指令存在 StringWriter writer = new StringWriter(); marshaller.marshal(t, writer); return writer.toString(); } public static void main(String[] args) throws JAXBException { //从xml文件读取 String filePath = "./src/vdi.xml"; Vdis vdis1 = Test.readFromFile(Vdis.class, filePath); System.out.println(vdis1.toString()); for(Vdi vdi : vdis1){ System.out.println(vdi); } //从xml字符串读取 StringBuffer xmlStr = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<vdis size=\"2\" batch_number=\"20141212123456\" errmsg=\"\">" + "<vdi>" + "<id>1</id>" + "<uuid>6524552158933218</uuid>" + "<name>disk1</name>" + "<disk_size>20</disk_size>" + "</vdi>" + "<vdi>" + "<id>2</id>" + "<uuid>6524552158933123</uuid>" + "<name>disk2</name>" + "<disk_size>40</disk_size>" + "</vdi>" + "</vdis>"); Vdis vdis2 = Test.readFromString(Vdis.class, xmlStr); System.out.println(vdis2.toString()); for(Vdi vdi : vdis2){ System.out.println(vdi); } //把对象转化成xml输出到控制台 Test.writeToConsole(Vdis.class, vdis2); //把对象转化成xml输出到文件 String filePath2 = "./src/test.xml"; Test.writeToFile(Vdis.class, vdis2, filePath2); //把对象转化成xml字符串 String xmlStr2 = Test.writeToString(Vdis.class, vdis2); System.out.println(xmlStr2); } }
注意:
用了@XmlAccessorType(XmlAccessType.FIELD)、@XmlElement、@XmlAttribute注解后,属性自己的getter/setter方法就会被忽略,去掉这些主键使用属性自己的getter/setter方法一样可以。
简洁版:
Vdis.java
package com.chinac.jaxb.parsexml; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "vdis") @XmlAccessorType(XmlAccessType.FIELD) public class Vdis extends ArrayList<Vdi>{ @XmlAttribute(name = "size") private int size; @XmlAttribute(name = "batch_number") private Long batchNumber; @XmlAttribute(name = "errmsg") private String errmsg; @XmlElement(name = "vdi") private List<Vdi> getVdis(){ return this; } @Override public String toString() { return "size:"+size + ",batchNumber:"+batchNumber + ",errmsg:"+errmsg + ""; } }
Vdi.java
package com.chinac.jaxb.parsexml; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @XmlAccessorType(XmlAccessType.FIELD) public class Vdi { @XmlElement(name = "id") private int id; @XmlElement(name = "uuid") private String uuid; @XmlElement(name = "name") private String name; @XmlElement(name = "disk_size") private int diskSize; @Override public String toString() { return "id:"+id + ",uuid:"+uuid + ",name:"+name + ",diskSize:"+diskSize; } }
自主版:
Vdis.java
package com.chinac.jaxb.parsexml; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "vdis") @XmlAccessorType(XmlAccessType.FIELD) public class Vdis extends ArrayList<Vdi>{ private int size; private Long batchNumber; private String errmsg; private List<Vdi> getVdis(){ return this; } public int getSize() { System.out.println("aaa"); return size; } public void setSize(int size) { System.out.println("bbb"); this.size = size; } public Long getBatchNumber() { return batchNumber; } public void setBatchNumber(Long batchNumber) { this.batchNumber = batchNumber; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } @Override public String toString() { return "size:"+size + ",batchNumber:"+batchNumber + ",errmsg:"+errmsg + ""; } }
Vdi.java
package com.chinac.jaxb.parsexml; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; public class Vdi { private int id; private String uuid; private String name; private int diskSize; public int getId() { System.out.println("ccc"); return id; } public void setId(int id) { System.out.println("ddd"); this.id = id; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDiskSize() { return diskSize; } public void setDiskSize(int diskSize) { this.diskSize = diskSize; } @Override public String toString() { return "id:"+id + ",uuid:"+uuid + ",name:"+name + ",diskSize:"+diskSize; } }