java使用jaxb操作xml示例
首先定义两个示例类classa,classb,用于后续的示例演示
package cn.lzrabbit;
public class classa {
private int classaid;
private string classaname;
private classb classb;
public int getclassaid() {
return classaid;
}
public void setclassaid(int classaid) {
this.classaid = classaid;
}
public string getclassaname() {
return classaname;
}
public void setclassaname(string classaname) {
this.classaname = classaname;
}
public classb getclassb() {
return classb;
}
public void setclassb(classb classb) {
this.classb = classb;
}
}
classa
package cn.lzrabbit;
public class classb {
private int classbid;
private string classbname;
public int getclassbid() {
return classbid;
}
public void setclassbid(int classbid) {
this.classbid = classbid;
}
public string getclassbname() {
return classbname;
}
public void setclassbname(string classbname) {
this.classbname = classbname;
}
}
classb
用于序列化的xmlutil
package cn.lzrabbit;
import java.io.stringreader;
import java.io.stringwriter;
import javax.xml.bind.*;
public class xmlutil {
public static string toxml(object obj) {
try {
jaxbcontext context = jaxbcontext.newinstance(obj.getclass());
marshaller marshaller = context.createmarshaller();
marshaller.setproperty(marshaller.jaxb_encoding, "utf-8");// //编码格式
marshaller.setproperty(marshaller.jaxb_formatted_output, true);// 是否格式化生成的xml串
marshaller.setproperty(marshaller.jaxb_fragment, false);// 是否省略xm头声明信息
stringwriter writer = new stringwriter();
marshaller.marshal(obj, writer);
return writer.tostring();
} catch (exception e) {
throw new runtimeexception(e);
}
}
@suppresswarnings("unchecked")
public static <t> t fromxml(string xml, class<t> valuetype) {
try {
jaxbcontext context = jaxbcontext.newinstance(valuetype);
unmarshaller unmarshaller = context.createunmarshaller();
return (t) unmarshaller.unmarshal(new stringreader(xml));
} catch (exception e) {
throw new runtimeexception(e.getmessage());
}
}
}
xmlutil
调用如下:
package cn.lzrabbit;
public class mainrun {
/**
* @param args
*/
public static void main(string[] args) {
classb classb = new classb();
classb.setclassbid(22);
classb.setclassbname("b2");
classa classa = new classa();
classa.setclassaid(11);
classa.setclassaname("a1");
classa.setclassb(classb);
system.out.println(xmlutil.toxml(classa));
}
}
mainrun
输出结果如下:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<classa>
<classaid>11</classaid>
<classaname>a1</classaname>
<classb>
<classbid>22</classbid>
<classbname>b2</classbname>
</classb>
</classa>
这里要注意以下几点
1要序列化的类加上 @xmlrootelement注解,否则会报错(错误提示很清晰,这里就不贴出来了)
2jaxb序列化xml时 默认序列化getter和setter,且getter和setter必须成对出现才会被序列化
3属性名称,默认序列化出来的类和属性名称默认是首字母转换为小写,若需要控制属性名称需要在getter或setter上使用 @xmlelement(name="classaid") 指定名称,这里要注意的是@xmlelement放置在getter或setter上都行,但只能放一个,也就是说不能同时在getter和setter上使用@xmlelement注解
4如何控制根节点名称?
使用@xmlrootelement指定name属性即可,如@xmlrootelement(name="classa")
5怎么添加命名空间
使用@xmlrootelement(namespace="cn.lzrabbit") 指定namespace属性
6怎么精确控制每个属性名称
jaxb自动转化为首字母小写会导致不可预料的属性名称出现, 不嫌麻烦的话为每个属性设置@xmlelement(name=""),想省事的话使用field
7怎么样实现序列化时使用field字段而不是使用setter和getter
在要使用的类上面加上@xmlaccessortype(xmlaccesstype.field)注解,并指定为xmlaccesstype.field,这里强烈推荐使用@xmlaccessortype(xmlaccesstype.field)注解,因为这样你可以精确的控制每个元素的名称,而不需要为每个属性去设置@xmlelement(name="")注解,当然也可以在field上使用@xmlelement注解
下面给出使用了使用如上注解后的代码示例
@xmlrootelement(namespace="cn.lzrabbit")
@xmlaccessortype(xmlaccesstype.field)
public class classa {
private int classaid;
@xmlelement(name="classaname")
private string classaname;
private classb classb;
public int getclassaid() {
return classaid;
}
public void setclassaid(int classaid) {
this.classaid = classaid;
}
public string getclassaname() {
return classaname;
}
public void setclassaname(string classaname) {
this.classaname = classaname;
}
public classb getclassb() {
return classb;
}
public void setclassb(classb classb) {
this.classb = classb;
}
}
@xmlrootelement
@xmlaccessortype(xmlaccesstype.field)
public class classb {
private int classbid;
private string classbname;
public int getclassbid() {
return classbid;
}
public void setclassbid(int classbid) {
this.classbid = classbid;
}
public string getclassbname() {
return classbname;
}
public void setclassbname(string classbname) {
this.classbname = classbname;
}
}
输出xml为
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns2:classa xmlns:ns2="cn.lzrabbit">
<classaid>11</classaid>
<classaname>a1</classaname>
<classb>
<classbid>22</classbid>
<classbname>b2</classbname>
</classb>
</ns2:classa>
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具: