Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB binding file: XmlAdapters and package name

Tags:

java

jaxb

xjc

I have a binding file like this

<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <jxb:bindings schemaLocation="example.xsd" node="/xs:schema">
    <jxb:schemaBindings>
        <jxb:package name="example" />
    </jxb:schemaBindings>
    <jxb:globalBindings>
        <jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
            printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
        <jxb:javaType name="java.util.Calendar" xmlType="xs:date"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
            printMethod="javax.xml.bind.DatatypeConverter.printDate" />
        <jxb:javaType name="java.util.Calendar" xmlType="xs:time"
            parseMethod="javax.xml.bind.DatatypeConverter.parseTime"
            printMethod="javax.xml.bind.DatatypeConverter.printTime" />
    </jxb:globalBindings>

  </jxb:bindings>
</jxb:bindings>

The schema class are generated in "example" (correct), but the XmlAdapters in "org.w3._2001.xmlschema" (wrong). How can I fix this?

like image 228
Puce Avatar asked Mar 17 '11 13:03

Puce


People also ask

What is JAXB binding file?

JAXB stands for Java Architecture for XML Binding.

What is XJB file?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

Why XSD is used in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

When parsing XML What is the default value of choice content property in the global binding attribute?

choiceContentProperty can be either true, false, 1, or 0. The default value is false.


1 Answers

For Apache CXF users, the cleanest way is to use the -p option offered by wsdl2java.

-p [wsdl-namespace=]PackageName

Specifies zero, or more, package names to use for the generated code. Optionally specifies the WSDL namespace to package name mapping.

In our case

-p http://www.w3.org/2001/XMLSchema=org.acme.foo

If you use the cxf-codegen-plugin, then just add another pair of <extraarg>.

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
        [...]
    <extraarg>-p</extraarg>
    <extraarg>http://www.w3.org/2001/XMLSchema=org.acme.foo</extraarg>
        [...]
</plugin>

No need for a targetNamespace pointing at the reserved XSD namespace and no need for catch-all jaxb package binding.

like image 186
Alain Pannetier Avatar answered Sep 29 '22 12:09

Alain Pannetier