首先定义两个示例类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>
更多java使用jaxb操作xml示例相关文章请关注PHP中文网!