使用XSTREAM将XML解析为ArrayList

时间:2014-05-19 18:30:18

标签: java xml arraylist xstream

我正在测试Xstream库。我想要的只是将XML解析为ArrayList这是tutorial我一直关注的。看起来很简单,但我的XML结构更复杂。因此,我脑子里出现了一些混乱。我一步一步地做了所有事情,得到了java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException

如果有人与此合作,请帮助。

这里是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>

这里有xml层次结构的类(好吧,我希望它是正确的):

    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
}

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());

1 个答案:

答案 0 :(得分:1)

XStream依赖于其他库。请确保您的类路径中有xmlpull和xpp3_min。使用Maven很容易:

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>

Maven会自动为您解决所有依赖关系。

此外,您的datamodel或xml中存在错误。请检查

    class Boards {
        @XStreamImplicit(itemFieldName = "id")

您可以在数据模型中将其设置为"ID"(大写)或xml中的id(小写)。

请同时检查

    @XStreamAlias("camber")
    private String camber;

您可以将别名设置为数据模型中的"camber_profile"或xml中的camber

最后但并非最不重要的是,我给你的建议是使用

    public void setIds(List<String> temp) {
        this.ids = new ArrayList<>(temp);
    }

    public List<String> getIds() {
        return Collections.unmodifiableList(ids);
    }

当您返回内部列表的引用时,调用者可以随意修改列表。这违反了良好的封装。