Search code examples
androidxmlretrofitsimple-frameworkhtml-escape-characters

Deserialize XML with HTML-escaped part using SimpleXML


I have an XML that looks something like this:

<xml>
&lt;example&gt;&lt;string&gt;test&lt;/string&gt;&lt;/example&gt;
</xml>

Is it possible to make SimpleXML restore those HTML-escaped characters during deserialization and continue as usual, inflating these classes:

@Root
public class Xml {

    @Element
    public Example example;
}

public class Example {

    @Element
    public String string;
}

Solution

  • Okay, I think I got it

    SimpleXML parses escaped sequence as a @Text, which I can use in constructor injection, and in that constructor I create another instance of Persister, which reads this sequence. Here is the code for the example in the question:

    @Root
    public class Xml {
        @Text
        String text;
    
        public Example example;
    
        public Xml(@Text String text) {
            Serializer serializer = new Persister();
            try {
                example = serializer.read(Example.class, text);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public class Example {
    
        @Element
        public String string;
    }
    

    What is interesting is that I don't need to restore escaped characters, SimpleXML does that for me.