JAXB 2.1 - Configure xs: any binding

I want to create java code from xsd using JAXB 2.1 XJC. I have an xsd schema and I cannot change it. I would like to use xjc mode : simple when creating Java classes from an xml schema.

There are elements in xsd:

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

As stated here: http://jaxb.java.net/guide/Mapping_of__xs_any___.html I expected these elements to be bound to:

 @XmlAnyElement(lax=true) public Object any; 

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

 @XmlAnyElement protected Element any; 

I tried to find a workaround, but everywhere he said that xs: any is processed without configuration. The only way to have xs: any element like java.lang.Object is to remove xjc: simple or change the processContents to "strict" in xsd. None of these options work for me now, since I cannot change the xml scheme, and I have some kind of legacy code that depends on the Java classes generated with xjc: simple mode, but now I need to use xs: any element, and I would like not to use org.w3c.dom.Element objects.

Any help would be greatly appreciated. Thanks.

+6
source share
1 answer

You can use the substitution plugin from JAXB2 Fundamentals . This allows you to configure weak / skipped / strict form 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 do not need to change the scheme for this, you can use this setting through the binding files.

+5
source

Source: https://habr.com/ru/post/908510/


All Articles