对象和XML相互转换

前端之家收集整理的这篇文章主要介绍了对象和XML相互转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//节点循序 @XmlType(propOrder = {"aa","ab","ac","ad"}) @XmlRootElement(name="XMLOBJA") public class XmlObja {

private String aa;

private String ab;

private String ac;

private XmlObjb ad;

//转换接口Name
@XmlElement(name = "AA") 
public String getAa() {
	return aa;
}

public void setAa(String aa) {
	this.aa = aa;
}


@XmlAttribute 
public String getAb() {
	return ab;
}

public void setAb(String ab) {
	this.ab = ab;
}

public String getAc() {
	return ac;
}

public void setAc(String ac) {
	this.ac = ac;
}
@XmlElement(name = "AD") 
public XmlObjb getAd() {
	return ad;
}

public void setAd(XmlObjb ad) {
	this.ad = ad;
}

}

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement public class XmlObjb {

private String ba;

private String bb;

private String bc;

public String getBa() {
	return ba;
}

public void setBa(String ba) {
	this.ba = ba;
}

public String getBb() {
	return bb;
}

public void setBb(String bb) {
	this.bb = bb;
}

public String getBc() {
	return bc;
}

public void setBc(String bc) {
	this.bc = bc;
}

}

public class XmlTest { public static void main(String[] args) { XmlObja xmlObja = new XmlObja(); xmlObja.setAa("中文"); xmlObja.setAb("a2"); xmlObja.setAc("a3"); XmlObjb xmlObjb = new XmlObjb(); xmlObjb.setBa("b1"); xmlObjb.setBb("b2"); xmlObjb.setBc("b3"); xmlObja.setAd(xmlObjb); JaxbUtil requestBinder = new JaxbUtil(XmlObja.class); //对象转XML String xml = requestBinder.toXml(xmlObja,"GBK"); System.out.println("-----------Obj to xml-----------"); System.out.println(xml); System.out.println("-----------xml to Obj-----------"); //XML转对象 XmlObja xmlObj =(XmlObja) requestBinder.fromXml(xml); System.out.println(xmlObj); } }

结果 -----------Obj to xml-----------

中文 a3 b1 b2 b3

-----------xml to Obj----------- com.test.bean.XmlObja@1a61172

原文链接:https://www.f2er.com/xml/294383.html

猜你在找的XML相关文章