Search code examples
xsdjaxb

Create XSD element with specific name but any structure


I am trying to create one internal element in my XSD that will render different types of XMLs

So expected XML structure will be like:

<root>
 <something>
 <specificName> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> etc </specificName>
 </something>
</root>

When I am using xs:any, the generated Java Class contains this

enter image description here

I want the name of the element to be specified, but to be of anyType.

The issue is that every element I am trying to set in the "any" element, is appended, rather than set, creating this XML structure:

<root>
 <something>
 <any> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> etc </any>
 </something> 
</root>

I want either:

  1. To let JaxB know somehow (by bindings?) that the element name will be set to "specificName"
  2. Ignore "any" element during the deserialization

PS: I know xs:anyType addresses the issue, but I want specifically to show that I will use any Namespace for this element with lax process context


Solution

  • Basically you answered yourself. You'll need an element which contains the any:

    public class SpecificName {
    
       @XmlAnyElement(lax = true)
       private Object any;
    
       ...
    }
    
    public class Something {
    
       @XmlElement
       private SpecificName specificName;
    
       ...
    }
    

    Note that having a second XML declaration as your example suggests doesn't work.