由於XML在分佈式系統中應用如此廣泛,而java又是開發網絡和分佈式系統可選的最佳運行環境之一,因此,java提供了很多的庫,使編程人員能夠輕鬆處理XML數據。在早期,java主要以JAXP定義的相關接口和類處理XML文檔。JAXP中包括基於樹結構的DOM和基於流方式的SAX和StAX。隨着面向對象技術的不斷髮展,像JAXP這樣低層次的XML處理方式,無法滿足日益增長的程序規模的需求,因此XOM(XML文檔和對象映射)被提出,Java通過對XOM的需求的研究(JSR222),提出了JAXB框架。JAXB框架主要是解決java對象與XML文檔映射問題。
1、編寫schema文檔
我們打算創建兩個數據模型,一個是student.xsd,描述了關於student的定義,一個是teacher.xsd描述teacher的定義。定義兩個xsd文件的目的是爲了解釋一個JAXBContent能包含多個根元素,並沒有特殊意義。
Student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</xsd:schema>
Teacher.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>
2. 使用xjc編譯生成java類
I:\programs\eclipse\SpringJAXBTest\src>xjc -p cn.edu.upc.upcgrid.guan.springjaxb
.student
xjc (XML to Java Compiler)是java提供的工具,在java安裝後就可以使用這個工具(注意要將java的環境變量配置正確),-p 後面的參數是生成類的包名和轉換的schema文件的路徑。
I:\programs\eclipse\SpringJAXBTest\src>xjc -p com.upc.upcgrid.guan.springjaxb.st
udent Teacher.xsd
Student.java類的內容大致如下:
package com.upc.upcgrid.guan.springjaxb.student;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="student")
public class Student {
}
生成的Teacher.java類的大致內容如下:
package com.upc.upcgrid.guan.springjaxb.student;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
})
@XmlRootElement(name = "teacher")
public class Teacher {
}
可以看出,xjc生成的Teacher和Student類只不過是一些get和set方法,並且添加了一些標記(在JAXB中,如果會使用這些標記,可以在自定義的類中使用這些標記,就不用定義schema)。
3. 配置Spring環境
這裏爲了簡單起見,我們不使用XML方式配置Spring,而是採用Annotation的方式對Spring進行配置。
這裏有兩個類,SpringConfigure用來替代以前的spring-servlet.xml(就是spring的配置文檔)。爲了方便使用,我們又創建一個spring的注入類MarshalAndUnmarshalServi
Spring的配置文件SpringConfigure.java
package com.upc.upcgrid.guan.springjaxb;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.upc.upcgrid.guan.springjaxb.student.Student;
import com.upc.upcgrid.guan.springjaxb.student.Teacher;
@Configuration
public class SpringConfigure
{
}
MarshalAndUnmarshalServi
package com.upc.upcgrid.guan.springjaxb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.stereotype.Component;
@Component
public class MarshalAndUnmarshalServi
}
4. 編寫測試程序。
package com.upc.upcgrid.guan.springjaxb;
import java.io.File;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicat
import org.springframework.oxm.XmlMappingException;
import com.upc.upcgrid.guan.springjaxb.student.Student;
import com.upc.upcgrid.guan.springjaxb.student.Teacher;
public class MainTest {
}
標準輸出:
2011-6-23 20:22:54 org.springframework.context.support.AbstractApplicationConte
信息:
Refreshing org.springframework.context.annotation.AnnotationConfigApplicat
2011-6-23 20:22:54 org.springframework.beans.factory.support.DefaultListableBeanFacto
信息:
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFacto
2011-6-23 20:22:54 org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromCla
信息: Creating JAXBContext with classes to be bound [class com.upc.upcgrid.guan.springjaxb.student.Student,class com.upc.upcgrid.guan.springjaxb.student.Teacher]
mary
Lucy
生成的XML文檔:
Student.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student id="21231">
</student>
Teacher.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<teacher>
</teacher>
程序結構:
參考:
Spring官方文檔
JAXB官方文檔:http://jaxb.java.net/tutorial/index.html