2

How can I support interleaved unknown XML using JAXB? Using @XmlAnyElement does not seem to be up to the job..

I am attempting to write an XML schema to allow interleaved elements from other namespaces, and use this with the built-in JAXB in Java 6. (I've not tried this fully with JAXB 2.2.1, as I don't have the luxury of requring my API users to do magic to endorse JAXB libraries, but I believe it does not work with 2.2.1 either)

So, for instance, given the schema anything.xsd, this example.xml should be valid:

<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://example.com/schema"
    xmlns:other="http://example.com/other">
    <rootFiles>
        <other:before>before rootFile</other:before>
        <rootFile path="test" />
        <other:after>after rootFile</other:after>
    </rootFiles>
</container>

Any XML from other namespaces should be allowed both before and after <rootFile>.


Schema-wise this is quite straight forward using <xsd:any> elements before and after the <xsd:element> for <rootFile>. Interleaved around optional elements is trickier, but that's a separate post. Trick is to make a wrapping group optional instead: <xsd:sequence minOccurs="0"><xsd:element minOccurs="1" .. /><xsd:any /></xsd:sequence>

I've added jaxb:property to specify different property names for the before/after any-properties. I had to do this to avoid XJC complaining about Property "Any" is already defined.

The problem is that JAXB seems to chuck all unknown elements of into the anyBefore property, as seen when marshalling out again the unmarshalled element (see the complete example Maven Project):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<container xmlns="http://example.com/schema">
    <rootFiles>
        <other:before xmlns:other="http://example.com/other">before rootFile</other:before>
        <other:after xmlns:other="http://example.com/other">after rootFile</other:after>
        <rootFile path="test"/>
    </rootFiles>
</container>

This makes it impossible to inspect rootFiles.getAnyAfter() to see which unknowns came where, but more seriously for me, impossible to serialize the XML out again without messing about the interleaved XML.

XJC does correctly create two any-properties:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "anyBefore",
        "rootFile",
        "anyAfter"
    })
    public static class RootFiles {

        @XmlAnyElement(lax = true)
        protected List<Object> anyBefore;
        @XmlElement(required = true)
        protected List<Container.RootFiles.RootFile> rootFile;
        @XmlAnyElement(lax = true)
        protected List<Object> anyAfter;

The JavaDoc for XmlAnyElement says:

This annotation is mutually exclusive with XmlElement, XmlAttribute, XmlValue, XmlElements, XmlID, and XmlIDREF. There can be only one XmlAnyElement annotated JavaBean property in a class and its super classes.

From this I've concluded that JAXB doesn't really care about the second @XmlAnyElement, and just places all direct unknowns into the first any property. I'm stuck as to what to do.

Note that my main concern is to preserve the unknown elements in the right location (the order seems to be preserved), I don't particularly care about parsing them, just ensure they are somewhat in the same place when serialising the XML again after some modifications to the elements.


Now you might wonder why I would design such a silly schema allowing random XML all over the place. It is unfortunately not my fault, I'm simply trying to manually convert this RELAX NG schema from the Adobe UCF specifications to an XML schema I can use with JAXB. The problem is this wording which allows any foreign XML elements and attributes in META-INF/container.xml:

Conforming UCF User Agents MUST ignore unrecognized elements (and their contents) and unrecognized attributes in a container.xml file, including unrecognized elements and unrecognized attributes from other namespaces.

Conforming container.xml files MUST be valid according to the RELAX NG UCF schema with the element as the root element after removing all elements (and child nodes of these elements) and attributes from other namespaces.

Now I could of course just delete any such elements as it says I can ignore them, but I thought that if in the future or some extension they meant something, at least I could preserve such elements correctly.. However I find this difficult using JAXB.

Are there any suggestions on how I can achieve this, like some magic properties to the Marshaller/Unmarshaller?

1 Answer 1

4

You can combine @XmlAnyElement with @XmlElementRef or @XmlElementRefs. Your annotations would look like:

@XmlElementRef(name="rootFile")
@XmlAnyElement
protected List<Object> beforeOrRootFileOrAfter;

In XML Schema, try <xs:choice maxOccurs="unbounded" .../>.

2
  • Thanks, that could work well enough. I'll have to iterate over the Object's to find the RootFile objects, but it has the added benefit of also exposing what 'others' are before or after a known object. Nov 24, 2010 at 17:10
  • Oh, it no longer requires at least one RootFile (minOccurs=1 on the choice would let you get away with just a single other) but I can check for that in code. Nov 24, 2010 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.