如何从XSD生成JAXB类?

我是一个XML的总新手。 我正在做一个Java EE项目的REST实现,我们返回了很多的XML。 有了这个,我们决定使用JAXB。 到目前为止,我们手动编码XML的模型。

但是,已经有这些复杂的结构,我们不知道如何编码。 我们已经阅读了关于从XSD生成类。 我们有一个XSD。

我的问题:

1.)我已经阅读了关于xjc,我在哪里可以find它?

2.)我们是否需要安装整个JAXB? (所以到目前为止我们使用的是不是这个JAXB?)

从Java SE 6开始,XJC包含在JDK的bin目录中。有关示例,请参阅:

博客的内容如下:

使用JAXB处理Atom Feed Atom是用于表示Web Feed的XML格式。 标准格式允许阅读器应用程序显示来自不同来源的提要。 在这个例子中,我们将处理这个博客的Atom提要。

演示

在这个例子中,我们将使用JAXB将与此博客对应的Atom XML提要转换为对象,然后再转换为XML。

import java.io.InputStream; import java.net.URL; import javax.xml.bind.*; import javax.xml.transform.stream.StreamSource; import org.w3._2005.atom.FeedType; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance("org.w3._2005.atom"); Unmarshaller unmarshaller = jc.createUnmarshaller(); URL url = new URL("http://bdoughan.blogspot.com/atom.xml"); InputStream xml = url.openStream(); JAXBElement<feedtype> feed = unmarshaller.unmarshal(new StreamSource(xml), FeedType.class); xml.close(); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(feed, System.out); } } 

JAXB模型

以下模型是由架构生成的Java编译器(XJC)。 我省略了get / set方法和注释以节省空间。

 xjc -d generated http://www.kbcafe.com/rss/atom.xsd.xml 

包信息

 @XmlSchema( namespace = "http://www.w3.org/2005/Atom", elementFormDefault = XmlNsForm.QUALIFIED) @XmlAccessorType(XmlAccessType.FIELD) package org.w3._2005.atom; import javax.xml.bind.annotation.*; 

CategoryType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "categoryType") public class CategoryType { @XmlAttribute(required = true) protected String term; @XmlAttribute @XmlSchemaType(name = "anyURI") protected String scheme; @XmlAttribute protected String label; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

内容types

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "contentType", propOrder = {"content"}) public class ContentType { @XmlMixed @XmlAnyElement(lax = true) protected List<Object> content; @XmlAttribute protected String type; @XmlAttribute @XmlSchemaType(name = "anyURI") protected String src; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

DateTimeType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.namespace.QName; @XmlType(name = "dateTimeType", propOrder = {"value"}) public class DateTimeType { @XmlValue @XmlSchemaType(name = "dateTime") protected XMLGregorianCalendar value; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

EntryType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "entryType", propOrder = {"authorOrCategoryOrContent"}) public class EntryType { @XmlElementRefs({ @XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "summary", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "source", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "content", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "published", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class) }) @XmlAnyElement(lax = true) protected List<Object> authorOrCategoryOrContent; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

的FeedType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "feedType", propOrder = {"authorOrCategoryOrContributor"}) public class FeedType { @XmlElementRefs({ @XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "entry", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class) }) @XmlAnyElement(lax = true) protected List<Object> authorOrCategoryOrContributor; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

GeneratorType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "generatorType", propOrder = {"value"}) public class GeneratorType { @XmlValue protected String value; @XmlAttribute @XmlSchemaType(name = "anyURI") protected String uri; @XmlAttribute protected String version; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

IconType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "iconType", propOrder = {"value"}) public class IconType { @XmlValue @XmlSchemaType(name = "anyURI") protected String value; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

IdType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "idType", propOrder = {"value"}) public class IdType { @XmlValue @XmlSchemaType(name = "anyURI") protected String value; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

链接types

 package org.w3._2005.atom; import java.math.BigInteger; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "linkType", propOrder = {"content"}) public class LinkType { @XmlValue protected String content; @XmlAttribute(required = true) @XmlSchemaType(name = "anyURI") protected String href; @XmlAttribute protected String rel; @XmlAttribute protected String type; @XmlAttribute @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NMTOKEN") protected String hreflang; @XmlAttribute protected String title; @XmlAttribute @XmlSchemaType(name = "positiveInteger") protected BigInteger length; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

标识

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "logoType", propOrder = {"value"}) public class LogoType { @XmlValue @XmlSchemaType(name = "anyURI") protected String value; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

PersonType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "personType", propOrder = {"nameOrUriOrEmail"}) public class PersonType { @XmlElementRefs({ @XmlElementRef(name = "email", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "name", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "uri", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class) }) @XmlAnyElement(lax = true) protected List<Object> nameOrUriOrEmail; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

SourceType中

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "sourceType", propOrder = {"authorOrCategoryOrContributor"}) public class SourceType { @XmlElementRefs({ @XmlElementRef(name = "updated", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "category", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "subtitle", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "logo", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "generator", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "icon", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "title", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "id", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "author", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "contributor", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "link", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class), @XmlElementRef(name = "rights", namespace = "http://www.w3.org/2005/Atom", type = JAXBElement.class) }) @XmlAnyElement(lax = true) protected List<Object> authorOrCategoryOrContributor; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

TextType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "textType", propOrder = {"content"}) public class TextType { @XmlMixed @XmlAnyElement(lax = true) protected List<Object> content; @XmlAttribute @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String type; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

UriType

 package org.w3._2005.atom; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.*; import javax.xml.namespace.QName; @XmlType(name = "uriType", propOrder = {"value"}) public class UriType { @XmlValue @XmlSchemaType(name = "anyURI") protected String value; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlSchemaType(name = "anyURI") protected String base; @XmlAttribute(namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "language") protected String lang; @XmlAnyAttribute private Map<QName, String> otherAttributes = new HashMap<QName, String>(); } 

对于Eclipse STS(至less3.5),你不需要安装任何东西。 右键单击schema.xsd – > Generate – > JAXB Classes。 你将不得不在下一步中指定包和位置,这就是你的类应该被生成的。 我想所有上述解决scheme的工作,但这似乎是迄今为止最简单的(对于STS用户)。

[更新] Eclipse STS版本3.6(基于开普勒)具有相同的function。

胡说

1)你可以使用标准的java工具xjc – ([你的java家目录] \ bin \ xjc.exe)。 但是您需要创build.bat(或.sh)脚本才能使用它。

例如generate.bat:

 [your java home dir]\bin\xjc.exe %1 %2 %3 

例如test-scheme.xsd:

 <?xml version="1.0"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://myprojects.net/xsd/TestScheme" xmlns="http://myprojects.net/xsd/TestScheme"> <xs:element name="employee" type="PersonInfoType"/> <xs:complexType name="PersonInfoType"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> 

用参数运行.bat文件:generate.bat test-scheme.xsd -d [你的src目录]

有关更多信息,请使用此文档 – http://docs.oracle.com/javaee/5/tutorial/doc/bnazg.html

这 – http://docs.oracle.com/javase/6/docs/technotes/tools/share/xjc.html

2)默认情况下,JAXB(xjc实用程序)与JDK6一起安装。

cxf对这类东西做了很好的支持

 <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-xjc-plugin</artifactId> <version>2.3.0</version> <configuration> <extensions> <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension> </extensions> </configuration> <executions> <execution> <id>generate-sources-trans</id> <phase>generate-sources</phase> <goals> <goal>xsdtojava</goal> </goals> <configuration> <sourceRoot>${basedir}/src/main/java</sourceRoot> <xsdOptions> <xsdOption> <xsd>src/main/resources/xxx.xsd</xsd> </xsdOption> </xsdOptions> </configuration> </execution> </executions> </plugin> 
  • 如果您使用的是Eclipse,那么也可以尝试使用JAXB Eclipse插件

  • 您可以在这里find关于jdk安装附带的XJC绑定编译器的更多信息:xjc: 用于XML绑定的Java™架构 – 绑定编译器

我希望这有帮助!

  1. 下载http://java.net/downloads/jaxb-workshop/IDE%20plugins/org.jvnet.jaxbw.zip
  2. 解压zip文件。
  3. 将org.jvnet.jaxbw.eclipse_1.0.0文件夹放入.eclipse \ plugins文件夹中
  4. 重新启动日食。
  5. 右键点击XSD文件,你可以find竞争菜单。 JAXB 2.0 – >运行XJC。

Eclipse ,右键单击想要获取的xsd文件 – > Generate – > Java … – > Generator:“Schema to JAXB Java Classes”。

我只是遇到了同样的问题,我有一堆xsd文件,其中只有一个是XML Root Element ,它在我之前在Eclipse中解释过

在intellij中单击.xsd文件 – > WebServices – >从Xml Schema JAXB生成Java代码,然后提供程序包path和程序包名称 – > ok

您可以从http://jaxb.java.net/2.2.5/下载JAXB jar文件。您不需要安装任何东西,只需调用xjc命令并使用classpath参数指向下载的JAXB jar文件即可。

你可以在这里看到我的问题如何从ANT执行JAXB编译器有一个使用ant的例子。