Bludwarf
Bludwarf

Reputation: 908

SimpleXML : any element matching

I need to parse an XML element that may have multiple format, depending on user needs. This format is implemented as "any" element in the XSD.

I have found that it is possible to do it with JAXB with @XmlAnyElement annotation :

@XmlAnyElement
protected List<Element> any;

But I would like to know how do it using Simple Xml framework. Is it possible ? Will I need to mix both JAXB and SimpleXml ?

Here is the same question on Simple support : http://ehc.ac/p/simple/mailman/message/33015962/

Upvotes: 1

Views: 759

Answers (1)

Andrea Richiardi
Andrea Richiardi

Reputation: 733

SimpleXml has the Element*Union feature exactly for this use case. Have a look at the following, which maps a list of interface Result to either the Result1 or Result2 implementation:

@Root(name = "response", strict = false)
public class Response {

    @ElementListUnion({
            @ElementList(inline = true, type = Result1.class, required=false),
            @ElementList(inline = true, type = Result2.class, required=false)
    })
    private @Nullable List<Result> resultList;

...

SimpleXml tries and binds one or the other implementation, that is why it is advisable to have a common interface for your getters. Of course you still need to write a model which matches with the xml in input in some way, but the union can help to reduce heterogeneous data input to your business/software domain.

Upvotes: 2

Related Questions