jaxb的解析xml和生成xml列子
解析XML文件:xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<trans>
<tran>
<requestId>111111</requestId>
<orderNo>13141322</orderNo>
<returnCode>000001</returnCode>
<returnInfo>交易失败</returnInfo>
</tran>
<tran>
<requestId>2222222</requestId>
<orderNo>13141323</orderNo>
<returnCode>000000</returnCode>
<returnInfo>交易成功</returnInfo>
</tran>
</trans>
建立对应的JAVA bean类
建立对应的JAVA bean类
Trans.java类
package com.jaxb.test;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "trans")
@XmlAccessorType(XmlAccessType.FIELD)
public class Trans{
@XmlElement(name = "tran")
private List<Tran> data = null;
public List<Tran> getData() {
return data;
}
public void setData(List<Tran> data) {
this.data = data;
}
}
Tran.java类
package com.jaxb.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Tran {
@XmlElement(name = "requestId")
private Long requestId;
@XmlElement(name = "orderNo")
private Long orderNo;
@XmlElement(name = "returnCode")
private String returnCode;
@XmlElement(name = "returnInfo")
private String returnInfo;
public Long getRequestId() {
return requestId;
}
public void setRequestId(Long requestId) {
this.requestId = requestId;
}
public Long getOrderNo() {
return orderNo;
}
public void setOrderNo(Long orderNo) {
this.orderNo = orderNo;
}
public String getReturnCode() {
return returnCode;
}
public void setReturnCode(String returnCode) {
this.returnCode = returnCode;
}
public String getReturnInfo() {
return returnInfo;
}
public void setReturnInfo(String returnInfo) {
this.returnInfo = returnInfo;
}
}
建立测试类
import java.io.File;
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 TestJAXB {
public static void main(String[] args) {
try {
//read xml file to Java object
Trans trans = TestJAXB.readString(Trans.class,"input.xml");
List<Tran> datas = trans.getDatas();
for (Tran o : datas) {
System.out.println(o.getReturnCode()+";"+o.getReturnInfo());
}
//write java object to xml
Tran tran = new Tran();
tran.setOrderNo(11111L);
tran.setRequestId(22222L);
tran.setReturnCode("0000");
tran.setReturnInfo("success");
Tran tran2 = new Tran();
tran2.setOrderNo(33333L);
tran2.setRequestId(44444L);
tran2.setReturnCode("0001");
tran2.setReturnInfo("failure");
Trans trxs = new Trans();
List<Tran> list = new ArrayList<Tran>();
list.add(tran);
list.add(tran2);
trxs.setDatas(list);
File file = new File("D:/output.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Trans.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(trxs, file);
jaxbMarshaller.marshal(trxs, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static <T> T readString(Class<T> clazz, String context) throws JAXBException {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(new File(context));
} catch (JAXBException e) {
throw e;
}
}
}