先定義兩個範例類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>
用於序列化的XmlUtil
@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 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>
輸出結果如下:rrreee
1要序列化的類別加上@XmlRootElement註解,否則會報錯(錯誤提示很清晰,這裡就不貼出來了)2JAXB序列化XML時 預設序列化getter和setter,且getter和setter必須成對出現才會被序列化3屬性名稱,預設序列化出來的類別和屬性名稱預設是首字母轉換為小寫,若需要控制屬性名稱需要在getter或setter上使用@XmlElement(name="ClassAId") 指定名稱,這裡要注意的是@XmlElement放置在getter或setter上都行,但只能放一個,也就是說不能同時在getter和setter上使用@XmlElement註解
4如何控制根節點名稱?
5怎麼加入命名空間
6怎麼精確控制每每一個每個屬性名稱
7怎麼樣實現序列化時使用Field欄位而不是使用setter和getter
rrreee
輸出xml為rrreee
更多java使用jaxb操作xml範例相關文章請關注PHP中文網! 🎜