简体   繁体   English

Jaxb:如何为XSD元素指定默认类

[英]Jaxb: How to specify a default class for an XSD element

When generating Java classes from a XSD, how can I specify that for some specific node, a specific and already existent Java class should be used instead of trying to generate one? 从XSD生成Java类时,如何指定某个特定节点使用特定且已经存在的Java类,而不是尝试生成一个Java类?

Thank you very much. 非常感谢你。

You should use following binding customization 您应该使用以下绑定自定义

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net" xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="../schema/yourSchema.xsd">

        <bindings node="//xs:complexType[@name='Foo']">
            <class ref="com.FooImpl"/> 
        </bindings>

    </bindings>
</bindings>

You can use episode file to reference the existing classes. 您可以使用episode文件来引用现有的类。 .episode files are just jaxb bindings file and has mappings between elements and java classes. .episode文件只是jaxb绑定文件,并且具有元素和Java类之间的映射。

a) if those existing classes are also generated from (another) xsd. a)如果这些现有类也从(另一个)xsd中生成。 use below option to first create .episode file. 使用以下选项首先创建.episode文件。

xjc -episode a.episode a.xsd

then use this a.episode that contains the mappings as input to the next xjc generation. 然后使用包含映射的a.episode作为下一代xjc输入。

xjc b.xsd -extension -b a.episode

b) If you want to refer some random classes, then you may have to write your own episode file providing mapping between element and class reference like below. b)如果要引用一些随机类,则可能必须编写自己的情节文件,以提供elementclass reference之间的映射,如下所示。

sample.episode 样本集

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
 <jaxb:bindings scd="x-schema::">
    <jaxb:bindings scd="employee">
      <jaxb:class ref="www1.example.Employee"/>
      <jaxb:package name="www1.example" />
    </jaxb:bindings>
   </jaxb:bindings>

and use xjc b.xsd -extension -b sample.episode 并使用xjc b.xsd -extension -b sample.episode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM