Reputation: 213
I'm testing out Xstream library. All I want is to parse XML into ArrayList This is a tutorial I've been following. It seems pretty simple, but my XML structure is more complex. Therefore there's some mess appeared in my mind. I did everything step by step and got java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
.
If someone worked with this please help.
Here's the XML boards.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<BOARDS>
<ID id="0">
<BRAND brand="Apo">
<NAME name="Supreme">
<description>
The APO Supreme is an interesting board. To start off I think someone filed down the edges because we have never experienced so little edge hold. It was to the point where it was a bit scary. Hopefully this was not how the board really rides because there is some snap and pop to it.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> Directional Twin </shape>
<camber_profile> Flat to Rocker </camber_profile>
<stance> Centered </stance>
<price> $499 </price>
<picture>drawable\\snb_apo_supreme.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="1">
<BRAND brand="Arbor">
<NAME name="Draft">
<description>
The Arbor Draft was the first snowboard in their line up to go rocker back in 2010. Over the years the Arbor Draft has pretty much remained the same board with the exception of a few minor refinements. This is one of the better jib park boards we came across and it also isn’t bad out of the jib park.
</description>
<riding_style> Jib / Street </riding_style>
<riding_level> Beginner - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Continuous Rocker </camber_profile>
<stance> Centered </stance>
<price> $399 </price>
<picture>drawable\\snb_arbor_draft.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="2">
<BRAND brand="Burton">
<NAME name ="Custom Restricted">
<description>
The Burton Custom Restricted is a Twin camber version of the Custom Flying V Restricted like the Custom is to the Custom Flying V. Usually the Restricted Custom is the same as the Custom but this time there is a completely different ride. They share the same name and the same twin shape but that’s it. The vastly different camber profiles change the ride a lot. This has a more semi-aggressive freestyle feel to it for those that like the Custom but want to ride switch a lot more. It’s still got the same mountain speed as the Custom Camber and it also has the carving ability. So if you like a more old school all mountain freestyle approach the Restricted Custom Twin might satisfy that need. It feels like a slightly more aggressive Burton Parkitect.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Advanced - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Traditional Camber </camber_profile>
<stance> Centered </stance>
<price> $549 </price>
<picture>drawable\\snb_burton_custom_restricted.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="3">
<BRAND brand="Capita">
<NAME name ="Defenders of Awesome">
<description>
The Capita Defenders Of Awesome is a hybrid camber with camber between the feet and then flat on the tip/tail where most have rocker. This makes for a ride that feels like a camber board but has a lot of pop for Ollie power and a carve. This is a fun but technical all mountain freestyle board for advanced but mainly expert riders. The 2013 and 2014 DOA are the same board except the 2014 has one extra small size.
</description>
<riding_style> Aggressive All Mountain Freestyle </riding_style>
<riding_level> Advanced - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Centered </stance>
<price> $379 </price>
<picture>drawable\\snb_capita_defenders_of_awesome.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="4">
<BRAND brand="DC">
<NAME name="Mega">
<description>
The DC Mega sounds like a board for riders with big feet but it isn’t. It was created for those that like the Ply but want more speed and aggression when it comes to all mountain freestyle riding. To us this is a dead competitor to the Capita DOA and the Salmon Villian. To us it feels like something in between the DOA and Villian. If you like a twin for the whole mountain then the Mega is a nice step up in terms of flex from the DC Ply.
</description>
<riding_style> Aggressive All Mountain Freestyle </riding_style>
<riding_level> Advanced - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Centered </stance>
<price> $430 </price>
<picture>drawab;e\\snb_dc_mega.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="5">
<BRAND brand="Gnu">
<NAME name="Billy Goat">
<description>
The Gnu Billy Goat has gone from being a semi loose ride for a wide variety of ability levels and returned to it’s aggressive all mountain past mainly for advanced to expert riders. This is all because of the change to the camber profile from C2 BTX to C3 BTX.
</description>
<riding_style> Aggressive All Mountain </riding_style>
<riding_level> Advanced - Expert </riding_level>
<shape> Directional </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Set Back </stance>
<price> $539 </price>
<picture>drawable\\snb_gnu_billy_goat.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="6">
<BRAND brand="Jones">
<NAME name="Mountain Twin">
<description>
The Jones Mountain Twin from 2014 and 2013 is a lot different than the 2011 and 2012 models. They had a centered stance and a very small camber between the feet that ended before the binding inserts. The Jones Mountain Twin 2014-2013 have an extended camber profile and set back the stance -20mm. It’s now just barely a directional twin but a much better All Mountain Ride. In addition the build quality is much better than the older models. It’s also probably our favorite all mountain/one board quiver rides out there. The 2013 and 2014 Jones Mountain Twin’s ride pretty much the same.
</description>
<riding_style> All Mountain </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> Directional Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Set Back </stance>
<price> $499 </price>
<picture>drawable\\snb_jones_mountain_twin.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="7">
<BRAND brand="K2">
<NAME name="WWW">
<description>
The K2 WWW offers good stability, solid ollie power, a catch free feel and a very fun all around jib park ride. We all really enjoyed the way this board helps you enjoy the park but most importantly the jib park.
</description>
<riding_style> Jib / Street </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Flat to Rocker </camber_profile>
<stance> Centered </stance>
<price> $399 </price>
<picture>drawable\\snb_k2_www.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="8">
<BRAND brand="Lib Tech">
<NAME name="Attack Banana">
<description>
The Lib Tech Attack Banana has a unique camber shape called EC2 BTX and it lives somewhere between C2 BTX and BTX. It’s like a combo between the Skate Banana and Gnu Riders Choice but with a much more mellow side cut. It has the loose feeling of the Skate Banana but the stiffer more poppy feel of the Riders Choice with less grip edge hold wise. It’s not for everyone but some might really like this new hybrid shape. The 2014 Lib Tech Attack Banana added some sizes so there is a 151n for smaller riders and women.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Rocker </camber_profile>
<stance> Centered </stance>
<price> $579 </price>
<picture>drawable\\snb_libtech_attack_banana.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="9">
<BRAND brand="Never Summer">
<NAME name="Evo">
<description>
The Never Summer Evo has a changed hybrid rocker profile for 2014. It’s got the same general look but the profile is extended and the nose/tail is not as kinked up. It’s not major but it’s major enough to make it the call over the 2013 and below models.
</description>
<riding_style> Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Rocker </camber_profile>
<stance> Centered </stance>
<price> $499 </price>
<picture>drawable\\snb_neversummer_evo.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="10">
<BRAND brand="Nidecker">
<NAME name="Advanced">
<description>
This is a board that I haven’t ridden for a few years now and despite a few minor tweaks and sizing changes it still seems like a good all mountain freestyle twin. It seems that they don’t have the Asymmetrical side cut any more or the Ultimate Grip in the side cut so it’s not quite like the Yes Greats (now called the Asym) any more. I’d like to ride this again but Nidecker doesn’t send this board to the US any more.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Centered </stance>
<price> $329 </price>
<picture>drawable\\snb_nidecker_advanced.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="11">
<BRAND brand="Nitro">
<NAME name="Pyro">
<description>
The Nitro Pyro is Nitro’s Asymmetrical board and we had a good time on it. It would be more fun if it had their hybrid camber they call Roof Chop Camber but it’s not a bad ride at all.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> Asymmetrical Twins </shape>
<camber_profile> Flat </camber_profile>
<stance> Centered </stance>
<price> $449 </price>
<picture>drawable\\snb_nitro_pyro.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="12">
<BRAND brand="Ride">
<NAME name="Kink">
<description>
The Ride Kink has a solid build that is made to deal with the jib park and not much outside of it. The Kink is pretty soft and playful. This is called low pro rocker but it’s more flat camber than rocker. What ever that means it really means that the Kink is pretty stable between Jibs and is fun to play with. The 2010 and 2011 Models are very similar. The 2012 seems to be a bit more stiff and they added an extra small size but it’s still pretty much the same board from 2011 and 2010. The 2013 and 2014 are pretty much the same too with the exceptions of minor tweaks here and there. This has a easy to throw around board and it’s got a stable between the foot kind of ride. It’s a pretty easy for any one to jib with and you could do a lot worse if you go with this board. It will appeal to a pretty wide variety of riders.
</description>
<riding_style> Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Flat to Rocker </camber_profile>
<stance> Centered </stance>
<price> $349 </price>
<picture>drawable\\snb_ride_kink.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="13">
<BRAND brand="Rome">
<NAME name="Hammerhead">
<description>
The Rome Hammerhead is a weird looking board. It’s a small jibber board with a huge nose/tail. Rome has made a lot of jibber/skate style boards this year and this is by far the weirdest. This is like the Shiv with a little extra going on at the the nose/tail. To us the Artifact Rocker would be a better choice but if you want to look very different then hammer head it up.
</description>
<riding_style> Jib / Street </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Flat to Rocker </camber_profile>
<stance> Centered </stance>
<price> $360 </price>
<picture>drawable\\snb_rome_hammerhead.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="14">
<BRAND brand="Rossignol">
<NAME name="Trickstick">
<description>
The Rossignol Trickstick is for sure a beginner park to all mountain freestyle board. I don’t expect or ask much from a beginner board. Most beginner riders just want an easy flex, easy board to one foot off the chair, flat base well and be a forgiving catch free ride. This checks all those boxes.
Since the addition of Amptek Technology (hybrid camber) the Rossignol Trickstick hasn’t changed much but it’s still a good board in this price range.
</description>
<riding_style> Freestyle </riding_style>
<riding_level> Beginner - Advanced </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Centered </stance>
<price> $299 </price>
<picture>drawable\\snb_rossignol_trickstick.jpg</picture>
</NAME>
</BRAND>
</ID>
<ID id="15">
<BRAND brand="Salomon">
<NAME name="Assassin">
<description>
The Salomon Aassassin is a new board with a hybrid camber profile that’s pretty mellow and fun except for one peculiar thing about it. It wants to auto butter on you at higher speed. Everything else about this board is really fun but it’s just something we haven’t experienced and were comfortable with.
</description>
<riding_style> All Mountain Freestyle </riding_style>
<riding_level> Intermediate - Expert </riding_level>
<shape> True Twin </shape>
<camber_profile> Hybrid Camber </camber_profile>
<stance> Centered </stance>
<price> $469 </price>
<picture>drawable\\snb_salomon_assassin.jpg</picture>
</NAME>
</BRAND>
</ID>
</BOARDS>
And here's class with xml hierarchy (well, I hope its correct):
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("BOARDS")
class Boards {
@XStreamImplicit(itemFieldName = "id")
private List ids = new ArrayList();
//region GETTERS-SETTERS
public void setIds(List temp){
this.ids = temp;
}
public List getIds(){
return ids;
}
// endregion
}
@XStreamAlias("ID")
class Id {
@XStreamAlias("id")
private String id;
@XStreamAlias("BRAND")
private Brand brand;
//region GETTER-SETTERS
public Brand getBrand(){
return brand;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public void setBrand(Brand brand){
this.brand = brand;
}
//endregion
}
@XStreamAlias("BRAND")
class Brand{
@XStreamAlias("brand")
private String brandName;
@XStreamAlias("NAME")
private Name name;
//region GETTERS-SETTER
public String getBrandName(){
return brandName;
}
public Name getName(){
return name;
}
public void setBrandName(String brandName){
this.brandName = brandName;
}
public void setName(Name name){
this.name = name;
}
//endregion
}
@XStreamAlias("NAME")
class Name{
@XStreamAlias("name")
private String boardName;
@XStreamAlias("description")
private String description;
@XStreamAlias("price")
private String price;
@XStreamAlias("shape")
private String shape;
@XStreamAlias("riding_level")
private String ridingLevel;
@XStreamAlias("riding_style")
private String ridingStyle;
@XStreamAlias("camber")
private String camber;
@XStreamAlias("stance")
private String stance;
@XStreamAlias("picture")
private String picture;
//region GETTER-SETTERS
public String getName(){
return boardName;
}
public String getDescription(){
return description;
}
public String getPrice(){
return price;
}
public String getRidingLevel(){
return ridingLevel;
}
public String getShape(){
return shape;
}
public String getCamber(){
return camber;
}
public String getStance(){
return stance;
}
public String getRidingStyle(){
return ridingStyle;
}
public String getPicture(){
return picture;
}
public void setBoardName(String name){
this.boardName = name;
}
public void setShape(String shape){
this.shape = shape;
}
public void setCamber(String camber){
this.camber = camber;
}
public void setRidingLevel(String ridingLevel){
this.ridingLevel = ridingLevel;
}
public void setRidingStyle(String ridingStyle){
this.ridingStyle = ridingStyle;
}
public void setPrice(String price){
this.price = price;
}
public void setStance(String stance){
this.stance = stance;
}
public void setDescription(String description){
this.description = description;
}
public void setPicture(String picture){
this.picture = picture;
}
//endregion
}
And the void main
:
FileReader reader = new FileReader("boards.xml"); // load file
XStream xstream = new XStream();
xstream.processAnnotations(Boards.class); // inform XStream to parse annotations in Data class
xstream.processAnnotations(Id.class); // and in two other classes...
xstream.processAnnotations(Brand.class); // we use for mappings
xstream.processAnnotations(Name.class); // we use for mappings
Boards boards = (Boards) xstream.fromXML(reader); // parse
// Print some data to console to see if results are correct
System.out.println("Number of boards = " + boards.getIds().size())
Id firstId = new Id();
System.out.println("First brand = " + firstId.getBrand());
Upvotes: 0
Views: 799
Reputation: 2712
XStream have dependencies on other libraries. Please make sure that you have xmlpull and xpp3_min in your classpath. It is easies to use Maven for that:
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
Maven automatically resolve all the dependencies for you.
Futhermore you have errors in your datamodel or xml. Please check
class Boards {
@XStreamImplicit(itemFieldName = "id")
Either you set it in the datamodel to "ID"
(uppercase) or to id
(lowercase) in the xml.
Please also check
@XStreamAlias("camber")
private String camber;
Either you set the alias to "camber_profile"
in the datamodel or to camber
in the xml.
Last but not least my advice to you is to use
public void setIds(List<String> temp) {
this.ids = new ArrayList<>(temp);
}
public List<String> getIds() {
return Collections.unmodifiableList(ids);
}
When you return the reference of an internal list, the caller can arbitrary modify the list. This violates a good encapsulation.
Upvotes: 1