How can I set the name of the class xml by constructor?

Posted by spderosso on Stack Overflow See other posts from Stack Overflow or by spderosso
Published on 2010-06-02T15:13:16Z Indexed on 2010/06/02 15:44 UTC
Read the original article Hit count: 274

Filed under:
|

Hi,

I want to be able to do something like this:

@Root(name="events")
class XMLEvents {

    @ElementList(inline=true)
    ArrayList<XMLEvent> events = Lists.newArrayList();

    XMLEvents(){
       ...
       events.add(new XMLEvent(time, type, professorP));
       events.add(new XMLEvent(time, type, student));
       events.add(new XMLEvent(time, type, course));
       ...
    }
}

The XMLEvent class to go something like:

class XMLEvent {

    @Root(name="professor")
    XMLEvent(DateTime time, LogType type, Professor p){
        ...
    }

    @Root(name="student")
    XMLEvent(DateTime time, LogType type, Student st){
        ...
    }

    @Root(name="course")
    XMLEvent(DateTime time, LogType type, Course c){
        ...
    }
}

For the output to be:

<events>
  <professor> ... </professor>
  <student> ... </student>
  <course> ... </course>
</events>

So depending on the constructor I call to create a new XMLEvent the root name to which is mapped is different. Is this even possible? Of course the past example was just to transmit what I need. Putting the @Root annotation there didn't change anything

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about simplexml