Search code examples
javaandroidxmlsimple-framework

Simple XML Framework Parser Issue - XML Parsing in Android


I'm having a problem with parsing XML in Android app, a question here implies that there are 4 types of XML parsing mechanism advised: - SAX - DOM - XmlPullParser - Simple XML Framework

While Simple Framework is amazing and I was already working with it I hit a dead end when I found out it can't support @Text and @Element in the same class, which sucks because I can't change my XML Scheme.

So ideas are appreciated and any advises will be great.


Solution

  • Well after researching for a long time I found the best XML parser suited to my needs was the JAXB Parser.

    • Supports complex types.
    • Annotation Oriented.
    • Works well with Android.
    • Very easy to understand.
    • Tools to generate JAXB annotated classes already exists in Java 1.6+

    An Example to show how easy it is to use:

    @XmlRootElement(name = "a")
     public class A {
    
    @XmlElementRefs({
        @XmlElementRef(name = "lang", namespace = "http://www.w3.org/namespace/", type = Lang.class, required = false),
        @XmlElementRef(name = "subst", namespace = "http://www.w3.org/namespace/", type = Subst.class, required = false),
        @XmlElementRef(name = "include", namespace = "http://www.w3.org/namespace/", type = Include.class, required = false),
        @XmlElementRef(name = "br", namespace = "http://www.w3.org/namespace/", type = Br.class, required = false),
        @XmlElementRef(name = "kw", namespace = "http://www.w3.org/namespace/", type = Kw.class, required = false),
        @XmlElementRef(name = "help", namespace = "http://www.w3.org/namespace/", type = Help.class, required = false)
    })
    @XmlMixed
    protected List<Object> content;
    @XmlAttribute(name = "cost")
    protected String cost;
    @XmlAttribute(name = "href", required = true)
    protected String href;
    @XmlAttribute(name = "key")
    protected String key;
    

    So that was the best I came up with.


    Any additions are welcome :)