我在 XML 方面完全是个新手。我正在做一个 JavaEE 项目 REST 实现,我们返回了大量的 XML。因此,我们决定使用 JAXB。到目前为止,我们手动为 XML 编写了模型代码。
但是已经存在这些我们不知道如何编码的复杂结构。我们已经阅读了关于从 XSD 生成类的文章。我们确实有 XSD。
我的问题是:
1)我读过 XJC,在哪里可以找到它?
2.)我们必须安装整个 JAXB 吗? (那么我们到目前为止使用的是 JAXB 吗?)
您可以从 < a href = “ http://JAXB.java.net/2.2.5/”rel = “ nofollow”> http://JAXB.java.net/2.2.5/下载 JAXB jar 文件 您不需要安装任何东西,只需调用 xjc 命令并使用类路径参数指向下载的 JAXBjar 文件。
如果您正在使用 Eclipse,您还可以尝试使用 JAXB Eclipse Plug-In
你可以在这里找到更多关于 XJC 绑定编译器的信息,这是 jdk 安装程序: XJC: < a href = “ http://docs.oracle.com/javase/6/docs/technology/tools/share/XJC.html”rel = “ norefrer”> JavaTM Architecture for XML Binding-Binding Compiler
希望这个能帮上忙!
从 Java SE 6开始,XJC 包含在 JDK 的 bin 目录中:
博客的内容如下:
用 JAXB 处理原子馈源 Atom 是一种表示 web 提要的 XML 格式。标准格式允许阅读器应用程序显示来自不同来源的提要。在这个示例中,我们将处理这个 blog 的 Atom 提要。
演示
在本例中,我们将使用 JAXB 将与此 blog 对应的 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.*;
类别类型
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>(); }
内容类型
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>(); }
日期时间类型
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>(); }
发电机类型
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>(); }
ID 类型
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>(); }
LinkType
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>(); }
LogoType
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>(); }
个人类型
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>(); }
文本类型
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>(); }
1)你可以使用标准的 java 工具 谢谢-([ your java home dir ] bin xjc.exe)。但你需要创造。蝙蝠(或。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 [ your src dir ]
更多信息请使用这个文档 -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一起安装。
对于 Eclipse STS (至少3.5),您不需要安装任何东西。你必须在下一步中指定包和位置,就是这样,你的类应该被生成。我想上面提到的所有解决方案都是有效的,但是这似乎是目前为止最简单的(对于 STS 用户)。
[更新] Eclipse STS 版本3.6(基于开普勒)提供了相同的功能。
在 Eclipse中,右键单击您想要获取的 xsd文件—— > Generate —— > Java... —— > Generator: “ Schema to JAXB Java Class”。
Eclipse
xsd
我刚刚面临同样的问题,我有一大堆 xsd文件,其中只有一个是 XML Root Element,它工作得很好,正如我在 Eclipse 中解释的那样
XML Root Element
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>
在 intellij click. xsd 文件中 - > 网上服务 - > 从 Xml Schema JAXB 生成 Java 代码 然后给出包路径和包名 - > ok
注意,所需的插件是: JavaEE: Web 服务(JAX-WS)(绑定)
您还可以使用 Jaxb2-maven-plugin插件从模式生成源代码:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <sources> <source>src/main/resources/your_schema.xsd</source> </sources> <xjbSources> <xjbSource>src/main/resources/bindings.xjb</xjbSource> </xjbSources> <packageName>some_package</packageName> <outputDirectory>src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> <generateEpisode>false</generateEpisode> <noGeneratedHeaderComments>true</noGeneratedHeaderComments> </configuration> </plugin>