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的约束,如:

   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:  } 
.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. 以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);
}
}
.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; }

其他功能

    本文提供的这个例子很简单,实际上JAXB还可以定义文档的哪些元素(属性)可以被转换成类,哪些被转换成类的属性。处理元素的属性。处理枚举值。为一些元素共同的子元素生成接口(因为JAXB不支持NameSpace),定义继承结构等等。

0 0
原创粉丝点击