JAXB tutorial question
in
Programming Questions
•
2 years ago
I posted a question in response to this post:
http://forum.processing.org/topic/jaxb-tutorial-xml-parsing-with-style
but I just realized that the OP was moved to Exhibition, so I thought I would re-post my question here. It is in reference to the JAXB tutorial on the wiki:
http://wiki.processing.org/w/XML_parsing_with_JAXB
I want to use JAXB for a data viz I'm working on, but having some difficulty getting the example working with my xml file, which I think may have to do with the way it's structured. The xml is a representation of a person's network of contacts (my contacts, my contacts' contacts, and so on), so it looks like this:
<contacts>
<level id="0">
<name>Brooke</name>
<level id="1">
<name>Katherine</name>
<level id="2">
<name>Marianne</name>
</level>
<name>Lize</name>
<name>Marina</name>
<level id="2">
<name>Chris</name>
<level id="3">
<name>Sam</name>
</level>
</level>
<name>Jillian</name>
</level>
</level>
</contacts>
I took the example from the Wiki and adapted MyURL.java to create a class for Level and Name. In AppConfig.java, I map the level elements to an ArrayList of type Level:
@XmlElement(name="level")
List<Level> levels=new ArrayList<Level>();
Then in the Level class I create a var for the id attribute, and map the name elements to an ArrayList of type Name:
@XmlAttribute(name="id")
int levelVal;
@XmlElement(name="name")
List<Name> names = new ArrayList<Name>();
In draw() I have a for loop:
for(Level l : config.levels ) {
println( l.levelVal );
for( Name n : l.names ) {
println( n.name );
}
}
but it only prints the first level (0) and name (Brooke). I'm trying to understand whether this could be due to the recursive structure of the xml and/or the way I've nested the Name class inside the Level class, or if I'm just misunderstanding the way the AppConfig class works? As always, greatly appreciate any tips or suggestions, thanks!
http://forum.processing.org/topic/jaxb-tutorial-xml-parsing-with-style
but I just realized that the OP was moved to Exhibition, so I thought I would re-post my question here. It is in reference to the JAXB tutorial on the wiki:
http://wiki.processing.org/w/XML_parsing_with_JAXB
I want to use JAXB for a data viz I'm working on, but having some difficulty getting the example working with my xml file, which I think may have to do with the way it's structured. The xml is a representation of a person's network of contacts (my contacts, my contacts' contacts, and so on), so it looks like this:
<contacts>
<level id="0">
<name>Brooke</name>
<level id="1">
<name>Katherine</name>
<level id="2">
<name>Marianne</name>
</level>
<name>Lize</name>
<name>Marina</name>
<level id="2">
<name>Chris</name>
<level id="3">
<name>Sam</name>
</level>
</level>
<name>Jillian</name>
</level>
</level>
</contacts>
I took the example from the Wiki and adapted MyURL.java to create a class for Level and Name. In AppConfig.java, I map the level elements to an ArrayList of type Level:
@XmlElement(name="level")
List<Level> levels=new ArrayList<Level>();
Then in the Level class I create a var for the id attribute, and map the name elements to an ArrayList of type Name:
@XmlAttribute(name="id")
int levelVal;
@XmlElement(name="name")
List<Name> names = new ArrayList<Name>();
In draw() I have a for loop:
for(Level l : config.levels ) {
println( l.levelVal );
for( Name n : l.names ) {
println( n.name );
}
}
but it only prints the first level (0) and name (Brooke). I'm trying to understand whether this could be due to the recursive structure of the xml and/or the way I've nested the Name class inside the Level class, or if I'm just misunderstanding the way the AppConfig class works? As always, greatly appreciate any tips or suggestions, thanks!
1