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

我的未来我决定 提交于 2019-12-25 07:59:52

问题


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?

Thank you very much.


回答1:


You can use episode file to reference the existing classes. .episode files are just jaxb bindings file and has mappings between elements and java classes.

a) if those existing classes are also generated from (another) xsd. use below option to first create .episode file.

xjc -episode a.episode a.xsd

then use this a.episode that contains the mappings as input to the next xjc generation.

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.

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




回答2:


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>


来源:https://stackoverflow.com/questions/39470716/jaxb-how-to-specify-a-default-class-for-an-xsd-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!