Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversation tree xml representation as java objects

I have a question about the recommended way to map a conversation tree to java objects. I'm looking at something like the following:

<conversation>
    <npcAction id="1" text="Some action text" userChoice="2" />
    <userChoice id="2">
        <availableAction>3</availableAction>
        <availableAction>4</availableAction>
    </userChocie>
    <userAction id="3" text="Some more action text." npcChoice="5" />
    <userAction id="4" text="Different action text." npcChoice="5" />
    <npcChoice id="5">
        <availableAction>6</availableAction>
        <availableAction>7</availableAction>
    </npcChoice>
    <npcAction id="6" text="Still more action text." userChoice="8" />
    <npcAction id="7" text="Still more action text." userChoice="8" />
    <userChoice id="8" />
</conversation>

When I visualize how I want to interact with this programmatically, however, I feel like I want to make something like this:

public class UserAction {
    String text;
    NpcChoice npcChoice;
}
public class NpcAction {
    String text;
    UserChoice userChoice;
}
public class UserChoice {
    ArrayList<UserAction> actions;
}
public class NpcChoice {
    ArrayList<NpcAction> actions;
}

In implementation, NpcAction and UserAction have other specific values that differentiate them, but to keep it simple I trimmed them down.

My question is whether there is an easy way to bind these representations together. I could make some intermediate object representation of the xml data and then re-map it manually in Java, but I have this feeling that I'm missing something about the best way to represent this information. Any suggestions would be much appreciated!

like image 490
Alex Pritchard Avatar asked Sep 13 '25 01:09

Alex Pritchard


1 Answers

You could do something like the following:

UserAction

public class UserAction {
    @XmlID 
    @XmlAttribute
    String id;

    @XmlAttribute String text;
    @XmlIDREF NpcChoice npcChoice;
}

NpcAction

public class NpcAction {
    @XmlID 
    @XmlAttribute
    String id;

    String text;
    UserChoice userChoice;
}

UserChoice

public class UserChoice {
    @XmlID 
    @XmlAttribute
    String id;

    @XmlElement(name="availableAction")
    ArrayList<UserAction> actions;
}

NpcChoice

public class NpcChoice {
    @XmlID 
    @XmlAttribute
    String id;

    @XmlElement(name="availableAction")
    ArrayList<NpcAction> actions;
}

For More Information

  • http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
like image 161
bdoughan Avatar answered Sep 14 '25 15:09

bdoughan