Jaxb教程
轉載自http://qmylove.spaces.live.com
Jaxb在J2EETurorial中有第16章有專門論述
Jaxb的Eclipse的插件https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html
可以方便的從schema生成java類,而不是像下文第三步中那樣用命令行。
開發步驟
1. 下載例如 jaxb-2_1_8.zip從 https://jaxb.dev.java.net/2.1.8/ 解壓,目錄結構如下
clip_image002
2. 定義xml文件的schema,用於定義xml文件的格式規範。本例爲GolfCountryClub.xsd
一個最簡單的schema
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/GolfCountryClub"
xmlns:tns="http://www.example.org/GolfCountryClub">
<element name="GolfCountryClub">
<complexType>
<sequence>
<element name="GolfCourse" type="tns:GolfCourseType"
maxOccurs="unbounded" minOccurs="1">
</element>
</sequence>
</complexType>
</element>
<complexType name="GolfCourseType">
<sequence>
<element name="Name" type="string">
</element>
</sequence>
<attribute name="NumberOfHoles" type="positiveInteger"
fixed="18">
</attribute>
</complexType>
</schema>
3. 運行xjc.bat或者xjc.sh腳本生成在schema中定義的java對象。
例如:解析GolfCountryClub.xsd並使用-p參數來指定包名
clip_image004
注:同樣,可以使用schemagen.bat工具來根據生成的Java類生成schema,例如
clip_image006
生成的schema1.xsd內容如下,和我們原schema基本一致。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GolfCountryClub">
<xs:complexType>
<xs:sequence>
<xs:element name="GolfCourse" type="GolfCourseType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="GolfCourseType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="NumberOfHoles" type="xs:positiveInteger"/>
</xs:complexType>
</xs:schema>
4. 使用javax.xml.bind.JAXB類的marshal靜態方法來根據java對象生成xml文件。
新建一個空的Flex項目,將3中生成的類複製到java 源代碼目錄中。並將解壓後lib文件夾下的jar文件複製到WEB-INFO/lib中。結構如圖。
clip_image008
我們的目標是生成一個最簡單的xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:GolfCountryClub
xmlns:ns2="http://www.example.org/GolfCountryClub">
<GolfCourse NumberOfHoles="18">
<Name>The best course</Name>
</GolfCourse>
</ns2:GolfCountryClub>
下面我們新建一個類來生成上面的xml文檔
package com.ibm.levin;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import com.ibm.levin.*;
public class CreateXML {
public static void main(String[] args) {
ObjectFactory factory = new ObjectFactory();
GolfCountryClub gcc = factory.createGolfCountryClub();
GolfCourseType gctype = factory.createGolfCourseType();
gctype.setName("The best course");
gctype.setNumberOfHoles(BigInteger.valueOf(18));
gcc.golfCourse = new ArrayList();
gcc.golfCourse.add(0, gctype);
System.out.println(marshall(gcc));
}
public static String marshall(Object jaxbObject) {
try {
JAXBContext jc = JAXBContext.newInstance("com.ibm.levin");
Marshaller marshaller = jc.createMarshaller();
Writer outputWriter = new StringWriter();
marshaller.marshal(jaxbObject, outputWriter);
return outputWriter.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
運行後即可輸出上述的xml文檔。
5. 實現javax.xml.bind.JAXB類的unmarshal靜態方法從xml文件裝載java對象。
略。
Jaxb在J2EETurorial中有第16章有專門論述
Jaxb的Eclipse的插件https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html
可以方便的從schema生成java類,而不是像下文第三步中那樣用命令行。
開發步驟
1. 下載例如 jaxb-2_1_8.zip從 https://jaxb.dev.java.net/2.1.8/ 解壓,目錄結構如下
clip_image002
2. 定義xml文件的schema,用於定義xml文件的格式規範。本例爲GolfCountryClub.xsd
一個最簡單的schema
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/GolfCountryClub"
xmlns:tns="http://www.example.org/GolfCountryClub">
<element name="GolfCountryClub">
<complexType>
<sequence>
<element name="GolfCourse" type="tns:GolfCourseType"
maxOccurs="unbounded" minOccurs="1">
</element>
</sequence>
</complexType>
</element>
<complexType name="GolfCourseType">
<sequence>
<element name="Name" type="string">
</element>
</sequence>
<attribute name="NumberOfHoles" type="positiveInteger"
fixed="18">
</attribute>
</complexType>
</schema>
3. 運行xjc.bat或者xjc.sh腳本生成在schema中定義的java對象。
例如:解析GolfCountryClub.xsd並使用-p參數來指定包名
clip_image004
注:同樣,可以使用schemagen.bat工具來根據生成的Java類生成schema,例如
clip_image006
生成的schema1.xsd內容如下,和我們原schema基本一致。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GolfCountryClub">
<xs:complexType>
<xs:sequence>
<xs:element name="GolfCourse" type="GolfCourseType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="GolfCourseType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="NumberOfHoles" type="xs:positiveInteger"/>
</xs:complexType>
</xs:schema>
4. 使用javax.xml.bind.JAXB類的marshal靜態方法來根據java對象生成xml文件。
新建一個空的Flex項目,將3中生成的類複製到java 源代碼目錄中。並將解壓後lib文件夾下的jar文件複製到WEB-INFO/lib中。結構如圖。
clip_image008
我們的目標是生成一個最簡單的xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:GolfCountryClub
xmlns:ns2="http://www.example.org/GolfCountryClub">
<GolfCourse NumberOfHoles="18">
<Name>The best course</Name>
</GolfCourse>
</ns2:GolfCountryClub>
下面我們新建一個類來生成上面的xml文檔
package com.ibm.levin;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import com.ibm.levin.*;
public class CreateXML {
public static void main(String[] args) {
ObjectFactory factory = new ObjectFactory();
GolfCountryClub gcc = factory.createGolfCountryClub();
GolfCourseType gctype = factory.createGolfCourseType();
gctype.setName("The best course");
gctype.setNumberOfHoles(BigInteger.valueOf(18));
gcc.golfCourse = new ArrayList();
gcc.golfCourse.add(0, gctype);
System.out.println(marshall(gcc));
}
public static String marshall(Object jaxbObject) {
try {
JAXBContext jc = JAXBContext.newInstance("com.ibm.levin");
Marshaller marshaller = jc.createMarshaller();
Writer outputWriter = new StringWriter();
marshaller.marshal(jaxbObject, outputWriter);
return outputWriter.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
運行後即可輸出上述的xml文檔。
5. 實現javax.xml.bind.JAXB類的unmarshal靜態方法從xml文件裝載java對象。
略。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.