package com.xiaomen.test.xiaomen;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
public class JaxbUtil {
public static String convertToXml(Object obj) {
return convertToXml(obj, "UTF-8");
}
public static String convertToXml(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static <T> T converyToJavaBean(String xml, Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
}
测试bean:
package com.xiaomen.test.xiaomen;
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ufinterface")
public class Ufinterface {
@XmlAttribute
private String account;
@XmlAttribute
private String billtype;
@XmlAttribute
private String businessunitcode;
@XmlAttribute
private String filename;
@XmlAttribute
private String groupcode;
@XmlAttribute
private String isexchang;
@XmlAttribute
private String replace;
@XmlAttribute
private String roottag;
@XmlAttribute
private String sender;
@XmlElement(name = "bill")
private List<Bill> billList;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getBilltype() {
return billtype;
}
public void setBilltype(String billtype) {
this.billtype = billtype;
}
public String getBusinessunitcode() {
return businessunitcode;
}
public void setBusinessunitcode(String businessunitcode) {
this.businessunitcode = businessunitcode;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getGroupcode() {
return groupcode;
}
public void setGroupcode(String groupcode) {
this.groupcode = groupcode;
}
public String getIsexchang() {
return isexchang;
}
public void setIsexchang(String isexchang) {
this.isexchang = isexchang;
}
public String getReplace() {
return replace;
}
public void setReplace(String replace) {
this.replace = replace;
}
public String getRoottag() {
return roottag;
}
public void setRoottag(String roottag) {
this.roottag = roottag;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public List<Bill> getBillList() {
return billList;
}
public void setBillList(List<Bill> billList) {
this.billList = billList;
}
}
package com.xiaomen.test.xiaomen;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "bill")
public class Bill {
@XmlElement
private Billhead billhead;
public Billhead getBillhead() {
return billhead;
}
public void setBillhead(Billhead billhead) {
this.billhead = billhead;
}
}
package com.xiaomen.test.xiaomen;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "billhead")
public class Billhead {
@XmlElement
private String creater;
@XmlElement
private String pk_group;
@XmlElement
private String pk_org;
@XmlElement
private String officialprintuser;
public String getCreater() {
return creater;
}
public void setCreater(String creater) {
this.creater = creater;
}
public String getPk_group() {
return pk_group;
}
public void setPk_group(String pk_group) {
this.pk_group = pk_group;
}
public String getPk_org() {
return pk_org;
}
public void setPk_org(String pk_org) {
this.pk_org = pk_org;
}
public String getOfficialprintuser() {
return officialprintuser;
}
public void setOfficialprintuser(String officialprintuser) {
this.officialprintuser = officialprintuser;
}
}
单元测试;
package com.xiaomen.test.xiaomen;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class Test01 {
@Test
public void tes01(){
Ufinterface ufinterface = new Ufinterface();
ufinterface.setAccount("develop");
ufinterface.setBilltype("F0");
ufinterface.setFilename("001");
List<Bill> billList = new ArrayList<Bill>();
Bill bill = new Bill();
Billhead billhead = new Billhead();
billhead.setCreater("001");
billhead.setOfficialprintuser("0101");
//billhead.setPk_group("ERP");
billhead.setPk_org("Pk_org");
bill.setBillhead(billhead);
billList.add(bill);
Bill bill1 = new Bill();
Billhead billhead1 = new Billhead();
billhead1.setCreater("001");
billhead1.setOfficialprintuser("0101");
billhead1.setPk_group("ERP");
bill1.setBillhead(billhead1);
billList.add(bill1);
billList.add(bill);
ufinterface.setBillList(billList);
String xml = JaxbUtil.convertToXml(ufinterface);
System.out.println(xml);
}
@Test
public void test02(){
String str ="<ufinterface account=\"develop\" billtype=\"F0\" filename=\"001\">\n" +
" <bill>\n" +
" <billhead>\n" +
" <creater>001</creater>\n" +
" <pk_group>ERP</pk_group>\n" +
" <pk_org>Pk_org</pk_org>\n" +
" <officialprintuser>0101</officialprintuser>\n" +
" </billhead>\n" +
" </bill>\n" +
" <bill>\n" +
" <billhead>\n" +
" <creater>001</creater>\n" +
" <pk_group>ERP</pk_group>\n" +
" <pk_org>Pk_org</pk_org>\n" +
" <officialprintuser>0101</officialprintuser>\n" +
" </billhead>\n" +
" </bill>\n" +
"</ufinterface>";
Ufinterface u = JaxbUtil.converyToJavaBean(str,Ufinterface.class);
System.out.println(u.getBillList().get(0).getBillhead().getPk_org());
}
}
本文介绍了一个使用Java JAXB进行XML与Java Bean互相转换的例子。通过具体的代码实现,展示了如何将Java对象序列化为XML字符串,以及如何从XML字符串反序列化为Java对象。
429

被折叠的 条评论
为什么被折叠?



