jaxb-xjc.jar_使用xjc在一秒钟内生成您的JAXB类

jaxb-xjc.jar

由于JAXB是JDK的一部分,因此它是处理XML文档最常用的框架之一。 它提供了一种从XML文档检索数据并将其存储到Java类的简便方法。 因为几乎每个Java开发人员都已经使用过JAXB,所以我不会解释不同的JAXB批注。 相反,我将重点介绍一个名为xjc的命令行工具,并向您展示如何基于现有XSD架构描述生成绑定类。

为现有XML接口实现所有绑定类可能是一项耗时且繁琐的任务。 但好消息是,您不需要这样做。 如果您具有XSD架构描述,则可以使用xjc绑定编译器创建所需的类。 甚至更好的是,xjc是JDK的一部分。 因此,不需要外部工具,如果需要,您应该始终使用它。

使用xjc

如下面的代码片段所示,xjc支持许多选项。 最重要的是:

  • -d定义生成的类应在文件系统中存储的位置
  • -p定义要使用的软件包,当然
  • -帮助,如果您还有其他需要。
Usage: xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ...
If dir is specified, all schema files in it will be compiled.
If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
Options:-nv                :  do not perform strict validation of the input schema(s)-extension         :  allow vendor extensions - do not strictly follow theCompatibility Rules and App E.2 from the JAXB Spec-b <file/dir>      :  specify external bindings files (each <file> must have its own -b)If a directory is given, **/*.xjb is searched-d <dir>           :  generated files will go into this directory-p <pkg>           :  specifies the target package-httpproxy <proxy> :  set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort-httpproxyfile <f> :  Works like -httpproxy but takes the argument in a file to protect password-classpath <arg>   :  specify where to find user class files-catalog <file>    :  specify catalog files to resolve external entity referencessupport TR9401, XCatalog, and OASIS XML Catalog format.-readOnly          :  generated files will be in read-only mode-npa               :  suppress generation of package level annotations (**/package-info.java)-no-header         :  suppress generation of a file header with timestamp-target (2.0|2.1)  :  behave like XJC 2.0 or 2.1 and generate code that doesnt use any 2.2 features.-encoding <encoding> :  specify character encoding for generated source files-enableIntrospection :  enable correct generation of Boolean getters/setters to enable Bean Introspection apis-contentForWildcard  :  generates content property for types with multiple xs:any derived elements-xmlschema         :  treat input as W3C XML Schema (default)-relaxng           :  treat input as RELAX NG (experimental,unsupported)-relaxng-compact   :  treat input as RELAX NG compact syntax (experimental,unsupported)-dtd               :  treat input as XML DTD (experimental,unsupported)-wsdl              :  treat input as WSDL and compile schemas inside it (experimental,unsupported)-verbose           :  be extra verbose-quiet             :  suppress compiler output-help              :  display this help message-version           :  display version information-fullversion       :  display full version informationExtensions:-Xinject-code      :  inject specified Java code fragments into the generated code-Xlocator          :  enable source location support for generated code-Xsync-methods     :  generate accessor methods with the 'synchronized' keyword-mark-generated    :  mark the generated code as @javax.annotation.Generated-episode <FILE>    :  generate the episode file for separate compilation

好的,让我们看一个例子。 我们将使用以下XSD模式定义和xjc来生成具有描述的属性和必需的JAXB批注的AuthorBook类。

<?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>

以下命令调用xjc,并为生成的类,包和XSD模式文件提供目标目录。

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

OK,操作成功完成,现在在src目录中有3个生成的类。 这可能比某些人预期的要多。 因此,让我们看看它们中的每一个。

类Author和Book看起来像预期的那样。 它们包含XSD架构中描述的属性和必需的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类。 它包含每个生成的类或接口的工厂方法。 如果您需要创建对象的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);}}

结论

我们研究了xjc并将其用于为现有XSD模式定义生成所需的绑定类。 xjc为每种复杂类型生成了一个类,并为简化JAXBElement表示形式的创建提供了附加的工厂类。

您如何看待xjc和生成的代码? 请给我留言并告诉我。

我认为该工具可生成非常干净的代码并节省大量时间。 在大多数情况下,可以将生成的代码直接添加到项目中。 但是,即使不是这种情况,基于生成的代码进行一些重构比自己做所有事情要快得多。

翻译自: https://www.javacodegeeks.com/2014/05/generate-your-jaxb-classes-in-a-second-with-xjc.html

jaxb-xjc.jar

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/341226.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

修改Typora默认的字体为好看秀美的中文字体“华康手札体“

一 下载并安装"华康手札体W5P"字体 &#xff08;1&#xff09;下载方式&#xff1a; 1. 方式一 http://www.downcc.com/font/341067.html2.百度云链接&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/125Hh-tqWOy1Ht-GvD_P2MQ 提取码&#xff1a;z6ka 复制…

测试Spring Boot有条件的合理方式

如果您或多或少有经验的Spring Boot用户&#xff0c;那么很幸运&#xff0c;在某些时候您可能需要遇到必须有条件地注入特定bean或配置的情况 。 它的机制是很好理解的 &#xff0c;但有时这样的测试条件下&#xff08;以及它们的组合&#xff09;可能会导致混乱。 在这篇文章中…

金山打字通答案

背景 金山打字通练习键盘键位&#xff0c;需要对第一关熟悉键盘知识进行答题。一下整理答案 答案 ABADCBCA

Onetab快速删除所有历史网页

1. 打开网页的控制台&#xff08;F12&#xff09; 2. 控制台粘贴该命令 document.querySelectorAll(div.deleteAllButton).forEach(function(ele, index, list){ele.click();});3. 然后按住回车&#xff08;有 conform 弹框需要确认&#xff09;&#xff0c;等待全部清空

golang的jwt学习笔记

文章目录 初始化项目加密一步一步编写程序另一个参数--加密方式关于StandardClaims 解密解析出来的怎么用关于`MapClaims`上面使用结构体的全代码实战项目关于验证这个项目的前端初始化项目 自然第一步是暗转jwt-go的依赖啦 #go get github.com/golang-jwt/jwt/v5 go get githu…

Ubuntu根目录下各文件夹的功能详细介绍

Ubuntu的根目录下存在着很多的文件夹&#xff0c;但你知道他们都存放着哪些文件呢&#xff1f;这些是深入了解Ubuntu系统必不缺少的知识&#xff0c;本文就关于此做一下介绍吧。 /bin/ 用以存储二进制可执行命令文件。 /sbin/ 许多系统命令的存储位置&#xff0c;/usr/sb…

mock测试使用断言_使用自定义断言丰富测试代码

mock测试使用断言受GeeCON会议期间tkaczanowski演讲的启发&#xff0c;我决定仔细研究AssertJ库的自定义断言。 在我的“骰子”游戏中&#xff0c;我创建了一个“机会”&#xff0c;它是骰子的任何组合&#xff0c;其分数是所有骰子的总和。 这是相对简单的对象&#xff1a; …

如何实现有序列表端内换行

背景 有序列表换行后自动开启下一个标号&#xff0c;让人苦恼。 操作 操作系统换行操作 1. 【Enter】键是硬回车&#xff0c;即段落标记。回车后文字属于下一段落 2. 【shiftEnter】是软回车快捷键。即人工换行符。回车后文字仍属于前一段落&#xff0c;只不过重新换行。 …

Java性能:For-eaching与Streaming

在for循环中向上或向下计数是最有效的迭代方式吗&#xff1f; 有时答案既不是。 阅读这篇文章&#xff0c;了解不同迭代品种的影响。 迭代性能 关于如何以高性能进行迭代有很多观点。 Java中的传统迭代方式是一个for循环&#xff0c;该循环从零开始&#xff0c;然后计数到一些…

好用的截图工具

目录 截图软件介绍 神器2——Snipaste PicPick——自带画图 LightShot——自带图床 功能总结 截图软件介绍 参考推荐5个截图工具&#xff08;超好用&#xff09; - 知乎&#xff0c;列出几个好用的截图工具 神器2——Snipaste 超级好用&#xff0c;QQ的功能Snipaste全都有…

Markdown语法与文本内容冲突的解决方案(软件Typora)

背景 使用Typora输入文字后&#xff0c;其中特殊字符自动识别为Markdown语法 解决方式 在使用markdown书写博客时&#xff0c;有可能会出现文本中的字符是markdown语法&#xff0c;那么markdown翻译器就会误将这些符号也翻译成某种功能&#xff0c;就会出现显示错误的情况。…

如何在Flutter(REST API)中进行API调用

在本文中&#xff0c;我们将探讨如何在波动中进行API调用并使用简单的REST API。 在这里查看我在Flutter上的其他一些帖子&#xff1a; Flutter vs React Native 了解Flutter中的BLoC架构 &#xff08;强烈建议&#xff09; 在Flutter中构建ListView&#xff08;RecyclerVi…

sublime关闭左边文件路径快捷键

目录 背景 解决方法 方案一&#xff1a; 方案二&#xff1a; 背景 sublime查看某一文件具体内容&#xff0c;左边文件路径占用一部分空间&#xff0c;影响观看 解决方法 方案一&#xff1a; 使用快捷键&#xff1a;关闭和打开相同&#xff0c;先按 CtrlK&#xff0c;再按…

Typora全局搜素

目录 背景 解决方式 全局文件夹下搜索 方法一 方法二 单一文件下搜索 查找功能 1 查找整个单词 ​2 区分大小写 背景 有时需要在打开的文件夹中所有文件搜索某一单词&#xff0c;有时需要在一个文件下搜索 解决方式 全局文件夹下搜索 方法一 方法二 快捷键&#x…

linux 内存不足杀进程_内存不足:杀死进程或牺牲孩子

linux 内存不足杀进程现在是早上六点。 我清醒地总结了导致我太早唤醒电话的事件顺序。 这些故事开始时&#xff0c;我的电话警报响了。 困倦而脾气暴躁的我检查了电话&#xff0c;看我是否真的疯了以至于无法在凌晨5点设置唤醒警报。 不&#xff0c;这是我们的监视系统&#x…

为什么声明性编码使您成为更好的程序员

在许多情况下&#xff0c;具有功能组成的声明式解决方案提供了优于传统命令式代码的优越代码度量。 阅读本文并了解如何使用具有功能组成的声明性代码成为更好的程序员。 在本文中&#xff0c;我们将仔细研究三个问题示例&#xff0c;并研究用于解决这些问题的两种不同技术&am…

Ubuntu 16.04设置IP、网关、DNS

说明&#xff1a;在网上给的教程上面通常会有这样的一个误导思路&#xff0c;按照配置文件设置后会不生效的问题&#xff0c;甚至没有一点效果&#xff0c;经过排查发现Linux下设置IP这个话题的入口线索应该分为两种&#xff1a;1为Server版&#xff0c;2为Desktop版&#xff0…

eclipse调试NS3

Tips&#xff1a; 1&#xff0c; 安装eclipse时注意选择C开发组件&#xff1b; 环境配置参考&#xff1a;https://www.cnblogs.com/zlcxbb/p/3852810.html 第一步&#xff0c;新建C工程&#xff1b; 第二步&#xff0c;在project explorer中右键属性&#xff0c;如下图&…

高效的企业测试-单元和用例测试(2/6)

在本系列的第一部分中&#xff0c;我们看到了有效测试应满足的一些普遍适用的原则和约束。 在这一部分中&#xff0c;我们将仔细研究代码级单元测试以及组件或用例测试。 单元测试 单元测试验证单个单元&#xff08;通常是类&#xff09;的行为&#xff0c;而忽略或模拟该单元…

sublime text 光标移动行末/行首

背景 使用Sublime有移动至行首与文件首部的需求 解决方式 sublime text没有直接跳转至行首行尾的&#xff0c;因为不能判断哪里是段首和短位。但可以通过连续移动单词的方式快速到达行首或行尾。 快捷键 左键 // 向左移动一个字母 右键 // 向右移动一个字母 ctrl左键 //…