使用xjc一秒钟生成您的JAXB类
由于JAXB是JDK的一部分,因此它是處理XML文檔最常用的框架之一。 它提供了一種從XML文檔檢索數(shù)據(jù)并將其存儲(chǔ)到Java類的簡便方法。 因?yàn)閹缀趺總€(gè)Java開發(fā)人員都已經(jīng)使用過JAXB,所以我不會(huì)解釋不同的JAXB批注。 相反,我將專注于一個(gè)名為xjc的命令行工具,并向您展示如何基于現(xiàn)有XSD架構(gòu)描述生成綁定類。
為現(xiàn)有XML接口實(shí)現(xiàn)所有綁定類可能是一項(xiàng)耗時(shí)且繁瑣的任務(wù)。 但好消息是,您不需要這樣做。 如果您具有XSD架構(gòu)描述,則可以使用xjc綁定編譯器創(chuàng)建所需的類。 甚至更好的是,xjc是JDK的一部分。 因此,不需要外部工具,如果需要,您應(yīng)該始終準(zhǔn)備好使用它。
使用xjc
如下面的代碼片段所示,xjc支持許多選項(xiàng)。 最重要的是:
- -d定義生成的類應(yīng)在文件系統(tǒng)中存儲(chǔ)的位置,
- -p定義要使用的軟件包,當(dāng)然
- -幫助,如果您還有其他需要。
例
好的,讓我們看一個(gè)例子。 我們將使用以下XSD模式定義和xjc來生成具有描述的屬性和必需的JAXB批注的Author和Book類。
<?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="author" type="author"/><xs:element name="book" type="book"/><xs:complexType name="author"><xs:sequence><xs:element name="firstName" type="xs:string" minOccurs="0"/><xs:element name="lastName" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType><xs:complexType name="book"><xs:sequence><xs:element ref="author" minOccurs="0"/><xs:element name="pages" type="xs:int"/><xs:element name="publicationDate" type="xs:dateTime" minOccurs="0"/><xs:element name="title" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType> </xs:schema>以下命令調(diào)用xjc,并為生成的類,包和XSD模式文件提供目標(biāo)目錄。
xjc -d src -p blog.thoughts.on.java schema.xsdparsing a schema... compiling a schema... blog\thoughts\on\java\Author.java blog\thoughts\on\java\Book.java blog\thoughts\on\java\ObjectFactory.java好的,操作成功完成,現(xiàn)在在src目錄中有3個(gè)生成的類。 這可能比某些人預(yù)期的要多。 因此,讓我們看看它們中的每一個(gè)。
類Author和Book看起來像預(yù)期的那樣。 它們包含XSD架構(gòu)中描述的屬性和必需的JAXB批注。
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2014.01.13 at 07:38:24 PM CET //package blog.thoughts.on.java;import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType;/*** <p>Java class for author complex type.* * <p>The following schema fragment specifies the expected content contained within this class.* * <pre>* <complexType name="author">* <complexContent>* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">* <sequence>* <element name="firstName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>* <element name="lastName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>* </sequence>* </restriction>* </complexContent>* </complexType>* </pre>* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "author", propOrder = {"firstName","lastName" }) public class Author {protected String firstName;protected String lastName;/*** Gets the value of the firstName property.* * @return* possible object is* {@link String }* */public String getFirstName() {return firstName;}/*** Sets the value of the firstName property.* * @param value* allowed object is* {@link String }* */public void setFirstName(String value) {this.firstName = value;}/*** Gets the value of the lastName property.* * @return* possible object is* {@link String }* */public String getLastName() {return lastName;}/*** Sets the value of the lastName property.* * @param value* allowed object is* {@link String }* */public void setLastName(String value) {this.lastName = value;}}// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2014.01.13 at 07:38:24 PM CET //package blog.thoughts.on.java;import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.datatype.XMLGregorianCalendar;/*** <p>Java class for book complex type.* * <p>The following schema fragment specifies the expected content contained within this class.* * <pre>* <complexType name="book">* <complexContent>* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">* <sequence>* <element ref="{}author" minOccurs="0"/>* <element name="pages" type="{http://www.w3.org/2001/XMLSchema}int"/>* <element name="publicationDate" type="{http://www.w3.org/2001/XMLSchema}dateTime" minOccurs="0"/>* <element name="title" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>* </sequence>* </restriction>* </complexContent>* </complexType>* </pre>* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "book", propOrder = {"author","pages","publicationDate","title" }) public class Book {protected Author author;protected int pages;@XmlSchemaType(name = "dateTime")protected XMLGregorianCalendar publicationDate;protected String title;/*** Gets the value of the author property.* * @return* possible object is* {@link Author }* */public Author getAuthor() {return author;}/*** Sets the value of the author property.* * @param value* allowed object is* {@link Author }* */public void setAuthor(Author value) {this.author = value;}/*** Gets the value of the pages property.* */public int getPages() {return pages;}/*** Sets the value of the pages property.* */public void setPages(int value) {this.pages = value;}/*** Gets the value of the publicationDate property.* * @return* possible object is* {@link XMLGregorianCalendar }* */public XMLGregorianCalendar getPublicationDate() {return publicationDate;}/*** Sets the value of the publicationDate property.* * @param value* allowed object is* {@link XMLGregorianCalendar }* */public void setPublicationDate(XMLGregorianCalendar value) {this.publicationDate = value;}/*** Gets the value of the title property.* * @return* possible object is* {@link String }* */public String getTitle() {return title;}/*** Sets the value of the title property.* * @param value* allowed object is* {@link String }* */public void setTitle(String value) {this.title = value;}}第三類,也許是意外類,是ObjectFactory類。 它包含每個(gè)生成的類或接口的工廠方法。 如果您需要?jiǎng)?chuàng)建對象的JAXBElement表示形式,這將非常有用。
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2014.01.13 at 07:38:24 PM CET //package blog.thoughts.on.java;import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.namespace.QName;/*** This object contains factory methods for each * Java content interface and Java element interface * generated in the blog.thoughts.on.java package. * <p>An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class.* */ @XmlRegistry public class ObjectFactory {private final static QName _Author_QNAME = new QName("", "author");private final static QName _Book_QNAME = new QName("", "book");/*** Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: blog.thoughts.on.java* */public ObjectFactory() {}/*** Create an instance of {@link Author }* */public Author createAuthor() {return new Author();}/*** Create an instance of {@link Book }* */public Book createBook() {return new Book();}/*** Create an instance of {@link JAXBElement }{@code <}{@link Author }{@code >}}* */@XmlElementDecl(namespace = "", name = "author")public JAXBElement<Author> createAuthor(Author value) {return new JAXBElement<Author>(_Author_QNAME, Author.class, null, value);}/*** Create an instance of {@link JAXBElement }{@code <}{@link Book }{@code >}}* */@XmlElementDecl(namespace = "", name = "book")public JAXBElement<Book> createBook(Book value) {return new JAXBElement<Book>(_Book_QNAME, Book.class, null, value);}}結(jié)論
我們研究了xjc并將其用于為現(xiàn)有XSD模式定義生成所需的綁定類。 xjc為每種復(fù)雜類型生成一個(gè)類,并為簡化創(chuàng)建JAXBElement表示形式提供一個(gè)附加的工廠類。
您如何看待xjc和生成的代碼? 請給我評論,并告訴我有關(guān)的信息。
我認(rèn)為該工具可生成非常干凈的代碼并節(jié)省大量時(shí)間。 在大多數(shù)情況下,可以將生成的代碼直接添加到項(xiàng)目中。 但是,即使不是這種情況,基于生成的代碼進(jìn)行一些重構(gòu)比自己做所有事情要快得多。
翻譯自: https://www.javacodegeeks.com/2014/05/generate-your-jaxb-classes-in-a-second-with-xjc.html
總結(jié)
以上是生活随笔為你收集整理的使用xjc一秒钟生成您的JAXB类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux刻录光盘命令(linux 刻录
- 下一篇: 内存不足:杀死进程或牺牲孩子