JAXB的使用
来源:互联网 发布:火眼金睛 软件 编辑:程序博客网 时间:2024/05/21 18:16
作用:
完成从xml生成java文件,或者从java生成xml的功能。 在jdk1.6版本中,已经在bin 下提供了 xjc.exe.(可根绝xsd和dtd 生成java文件)
步骤:
1. 编写xsd
2. 使用xjc 根据 xsd生成 java文件
3. 编译java文件,并在程序中使用, 通过marshal方法将对象的数据写进XML文件,通过unmarshal方法将XML文件的数据读入对象,通过validate方法验证XML文件是否符合DTD的约束,如:
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }1: 文件Test.java
2: import java.io.*;
3: import java.util.*;
4: import javax.xml.bind.*;
5: import javax.xml.marshal.*;
6: public class Test{7: public static void main(String[] args) throws Exception{8: BookList bl = new BookList();9: FileInputStream fis = new FileInputStream("exampleA.xml");10: try{11: bl = bl.unmarshal(fis);
12: }finally{13: fis.close();
14: }
15: List books = bl.getBook();
16: Book b = (Book)books.get(0);
17: b.setAuthor("王五");18:
19: bl.validate(); //先验证,不然marshal会出错20: FileOutputStream fos = new FileOutputStream("exampleB.xml");21: XMLWriter xw = new XMLWriter(fos,"GBK");22: try{23: bl.marshal(xw);
24: }finally{25: fos.close();
26: }
27: }
28: }
优缺点
1. 以java对象方式进行xml内容的读写;并对xml提供验证功能
2. 只能访问特定的(也就是你用DTD定义的)XML文档
数据类型转换
你可能已经注意到在上面的例子中,生成的Book对象的getPrice方法返回的是String,实际上它应该是float。同样publishDate以该是日期类型,而不是字符串。这是因为我们的绑定模式写得太简单了,模式编译器生成了默认的String类型。现在我们这样写:
文件bookList2.xjs
<xml-java-binding-schema version="1.0-ea"> <element name="bookList" type="class" root="true"/> <element name="price" type="value" convert="float"/> <element name="publishDate" type="value" convert="TransDate" /> <conversion name="TransDate" type="java.util.Date" parse="TransDate.parseDate" print="TransDate.printDate"/></xml-java-binding-schema>
bookList2.xjs第3行将Price转换成了float类型,float类型是一个简单类型,因此用convert="float"描述就可以了。而 publishDate需要转变成java.util.Date,这是一个类,而且他没有以字符串作为参数的构造函数。parse="TransDate.parseDate"就表示使用unmarshal读取数据的时候,会调用TransDate.parseDate()方法。这个静态方法以字符串为参数,返回java.util.date。print="TransDate.printDate"的作用相反。TransDate这个类需要我们提供。
文件TransDate.java
import java.util.Date;
public class TransDate {
private static java.text.SimpleDateFormat df= new java.text.SimpleDateFormat("yyyy-MM-dd");
public static Date parseDate(String d) {
try {
return df.parse(d);
} catch (java.text.ParseException pe) {
System.out.print(pe);
return new Date();
}
}
public static String printDate(Date d) {
return df.format(d);
}
}
其他功能
本文提供的这个例子很简单,实际上JAXB还可以定义文档的哪些元素(属性)可以被转换成类,哪些被转换成类的属性。处理元素的属性。处理枚举值。为一些元素共同的子元素生成接口(因为JAXB不支持NameSpace),定义继承结构等等。
- JAXB的使用
- JAXB的使用
- jaxb 的使用介绍[转载]
- JAXB注解的使用详解
- JAXB注解的使用详解
- JAXB 使用
- JAXB使用
- jaxb使用
- JAXB使用
- jaxb 使用遇到的问题解决的方案
- JAXB的使用(快速入门)
- JAXB的使用陷阱,CPU居高不下
- 使用jaxb-xjc的xjc和schemagen
- 使用jaxb-xjc的xjc和schemagen
- jaxb经常使用的注解标签
- jaxb的@xmlSchema注解使用没有效果
- jaxb 使用 之 marshal
- jaxb 使用之 unmarshall
- scanf
- tomcat无法发布war包
- ZeroCopy&sendFile
- Ajax跨域
- KMP算法实现strstr()函数
- JAXB的使用
- Java语言学习要点(001) JAVA中的权限控制符 注意的要点 protected class和成员
- robotframework中变量的重复使用
- 程序员接私单操作流程。
- 思科英文词汇
- Qt中QWidget加入到父类QWidget不能显示的问题
- 关于UIText换行
- Yii框架学习CDbCriteria查询条件收集
- HttpClient 4.5