1

I'm developing android project which is based on Simple Framework. For some time I noticed that I must to implement dynamic parsing. In brief I have a big xml file downloaded from backend. This xml file can contains tags which can be different from time to time and unchangeable tags which are used by my application.

Let's say that I have this xml file:

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
  <age>33</age>
</note>

And java model class

@Root(name="note", strict = false)    
public class Note {
    @Element(name = "to", required = false)
    public String to;
    @Element(name = "from", required = false)
    public String from;
    @Element(name = "heading", required = false)
    public String heading;
    @Element(name = "body", required = false)
    public String body;
    @Element(name = "age", required = false)
    public int age;

    //getters/setters...   
}

but sometimes I can download xml which can looks like this (x1 - unknown name):

<note>
  <x1>content...</x1>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <x2>content...</x2>
  <body>Don't forget me this weekend!</body>
  <age>33</age>
  <x3>content...</x3>
  <xN>content...</xN>
</note>

In that case when my app read/edit this xml and save to xml file to send back to backend there is no x1..xN tags because I don't known how to maintain them in my model.

My app is based on big model layer which contains that pojo classes so I need to find a solution where store this unknown tags.

1 Answer 1

1

You can try to create a custom NoteConverter.

public class NoteConverter implements Converter<Note> {

    public Note read(InputNode node) {
        // manually read all nodes
        // assign values to members: to, from, heading, body, age
        // other values save in some structure ex. Map inside the Note element

        return note;
   }

   public void write(OutputNode node, Note note) {
        // manually write note into outputNode
        // first write members: to, from, heading, body, age
        // finally write other nodes stored in map created in read function
   }

}

Annotate Note with @Convert(NoteConverter.class)
Add AnnotationStrategy to your serializer

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, if conventers are the only way to resolve this problem I will try to do that and I give you my result. Also I have one more question, what in case when I have another class object with conventer which is nested in Note class? Is second conventer will run first and return ready to use (with xml values) object to Note conventer ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.