Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB 2.1 - Customize xs:any binding

Tags:

java

jaxb

xsd

xjc

I want to generate java code from xsd using JAXB 2.1 XJC. I have an xsd schema provided and I can't change it. I would like to use xjc:simple mode while generating java classes from xml schema.

In the xsd there are elements:

<xs:any namespace="##other" processContents="lax"/>

As it is stated here: http://jaxb.java.net/guide/Mapping_of__xs_any___.html I expected that these elements will be binded to:

@XmlAnyElement(lax=true)
public Object any;

but when I use simple binding mode xjc:simple I have:

@XmlAnyElement
protected Element any;

I was trying to find a workaround, but everywhere it's said that xs:any is handled with no configuration. The only way to have xs:any element as java.lang.Object is to drop xjc:simple or change processContents to "strict" in xsd. None of these options are acceptable right now for me as I can't change xml schema and I have some legacy code that depends on java classes generated with xjc:simple mode but now I need to use xs:any element and I would like to avoid using org.w3c.dom.Element objects.

Any help would be very appreciated. Thanks.

like image 623
arek Avatar asked Feb 15 '12 12:02

arek


1 Answers

You can use the Wildcard plugin from JAXB2 Basics. This allows you to customize lax/skip/strict wildcard binding modes:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:wildcard="http://jaxb2-commons.dev.java.net/basic/wildcard"
    jaxb:version="2.1"
    jaxb:extensionBindingPrefixes="wildcard">

...

    <xs:complexType name="issueJIIB10Type" mixed="true">
        <xs:annotation>
            <xs:appinfo>
                <wildcard:lax/>
            </xs:appinfo>
        </xs:annotation>
        <xs:complexContent mixed="true">
            <xs:extension base="xs:anyType"/>
        </xs:complexContent>
    </xs:complexType> 

...

</xs:schema>

You don't have to change the schema for this, you can use this customization via binding files.

like image 184
lexicore Avatar answered Sep 20 '22 11:09

lexicore