jaxb接口学习实践

jdk带了支持jaxb的包,为javax.xml.bind.*; ,主要用到该包中的JAXBContext、Marshaller、UmShaller对象。其中Mashaller对象中的mashal方法负责
将将xml对象树写道流或者文件中,而UnMarshaller对象中的unMarshal方法负责将流或者文件中的内容转换成java对象树。其中unMarshal方法返回的是
JAXBElement<xml模式中根元素的类型>,输入是xml文件或者InputStream流。Marshal方法输入是JAXBElement<根元素类型>和文件或者OutputStream流对象。
所以要调用上述两个方法时,都要用到JAXBElement<根元素类型>元素,建立方法为ObjectFactory factory=new ObjectFactory();factory.create(根元素)(根元素类型 变量)

实例程序如下:

(1)首先建立XMLSchema文档(存储路径为E:\jing\java\workplace\JAXB\schema\)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog" type="catalogType"/>
<xsd:complexType name="catalogType">
   <xsd:sequence>
    <xsd:element ref="journal"   minOccurs="0" maxOccurs="unbounded"/>
   </xsd:sequence>
   <xsd:attribute name="section" type="xsd:string"/>
   <xsd:attribute name="publisher" type="xsd:string"/>
</xsd:complexType>
<xsd:element name="journal" type="journalType"/>
<xsd:complexType name="journalType">
   <xsd:sequence>
    <xsd:element ref="article"   minOccurs="0" maxOccurs="unbounded"/>
   </xsd:sequence>
</xsd:complexType>
<xsd:element name="article" type="articleType"/>
<xsd:complexType name="articleType">
   <xsd:sequence>
    <xsd:element name="title" type="xsd:string"/>
    <xsd:element name="author" type="xsd:string"/>
   </xsd:sequence>
   <xsd:attribute name="level" type="xsd:string"/>
   <xsd:attribute name="date" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
(2)利用JAXB形成对应于该XMLSchema的java类,执行的命令如下(也可以将如下命令写入Bat文件中,需要时只要双击运行bat文件就行,比较方便):

xjc -d "E:\jing\java\workplace\JAXB" -p "jing" "E:\jing\java\workplace\JAXB\schema\messages.xsd"

命令解释如下:

-d:代表将生成的类放在什么路径下

-p;代表生成的类所属于的包的名字

最后的引号的内的内容为所有用到的XMLSchema(包括存放的路径)

执行完上述命令后,如果你的XMLSchema文档没有语法错误就在名字为jing的包内生成该Schema对应的一些java类了,上述Schema文件生成的java类如下:

ArticleType.java

CatalogType.java

JournalType.java

ObjectFactory.java

(3)接下来可以用上述类来编程了

编写一个专门用来转换java类和XML文件之间的中间类,方便其他模块使用

package jing;
import java.io.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.net.*;
public class CatalogTypeUtils {
private static JAXBContext jaxbContext = null;
private static Marshaller marshaller = null;
private static ObjectFactory factory = null;


public static void buildXml(CatalogType catalogType, OutputStream out) {

   try {
    jaxbContext = JAXBContext.newInstance("jing");
    marshaller = jaxbContext.createMarshaller();
    ObjectFactory factory = new ObjectFactory();
    JAXBElement<CatalogType> catalogElement = factory.createCatalog(catalogType);
    //OutputStream out=socket.getOutputStream();
    marshaller.marshal(catalogElement,out);
    //out.close();
   }/* catch (IOException d) {
    d.printStackTrace();
   } */catch (JAXBException e) {
    e.printStackTrace();
   }
}

public static CatalogType buildObject(InputStream in) {
   JAXBElement<CatalogType> catalogElement = null;
   try {                                          
    JAXBContext jaxbContext = JAXBContext.newInstance("jing");
    Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
    //InputStream in=socket.getInputStream();
    catalogElement = (JAXBElement<CatalogType>) unMarshaller.unmarshal(in);
   } catch (Exception e) {
    e.printStackTrace();
   }
   return catalogElement.getValue();
}
}

说明:其中void buildXml(CatalogType catalogType, OutputStream out)负责将由catalogType为的根元素的xml文档转到out输出流中

          其中buildObject(InputStream in)负责将输入流中的内容转换成java对象

(4)应用程序

package jing;

import java.io.File;

public class Main {
private ObjectFactory factory = new ObjectFactory();
/*
   * @param xmlDocument is the output xml file
   */
public void testObject2Xml(File xmlDocument) { 
    CatalogType catalog = factory.createCatalogType();
    assmbleCatalogForTest(catalog);
    CatalogTypeUtils.buildXml(catalog,xmlDocument);
}
/*
   * @param xmlDocument is the source xml file for generating Object
   */
public CatalogType testXml2Object(File xmlDocument){
   return CatalogTypeUtils.buildObject(xmlDocument);
}
/*
   * This function is used to assmble CatalogType object for test
   */
private CatalogType assmbleCatalogForTest(CatalogType catalog){
   catalog.setSection("Java Technology");
   catalog.setPublisher("IBM developerWorks");

   JournalType journal = factory.createJournalType();
   ArticleType article = factory.createArticleType();

   article.setLevel("Intermediate");
   article.setDate("January-2004");
   article.setTitle("Service Oriented Architecture Frameworks");
   article.setAuthor("Naveen Balani");

   java.util.List journalList = catalog.getJournal();
   journalList.add(journal);
   java.util.List articleList = journal.getArticle();
   articleList.add(article);

   article = factory.createArticleType();
   article.setLevel("Advanced");
   article.setDate("October-2003");
   article.setTitle("Advance DAO Programming");
   article.setAuthor("Sean Sullivan");
   articleList = journal.getArticle();
   articleList.add(article);

   article = factory.createArticleType();
   article.setLevel("Advanced");
   article.setDate("May-2002");
   article.setTitle("Best Practices in EJB Exception Handling");
   article.setAuthor("Srikanth Shenoy");

   articleList = journal.getArticle();
   articleList.add(article);
   return catalog;
}

public static void main(String[] argv) {
   Main test = new Main();
   test.testObject2Xml(new File("E:\\hello.xml"));
   CatalogType type = test.testXml2Object(new File("E:\\hello.xml"));
   System.out.println(type.getPublisher());
}
}




你可能感兴趣的:(java,DAO,编程,xml,socket)