2014-05-19 5 views
0

Xstream 라이브러리를 테스트하고 있습니다. 내가 원하는 것은 XML을 ArrayList로 파싱하는 것이다. 이것은 내가 수행 한 tutorial이다. 그것은 매우 단순 해 보이지만 제 XML 구조는 더 복잡합니다. 그러므로 내 마음 속에 약간의 혼란이 나타났습니다. 나는 모든 걸 단계적으로하고 java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException을 얻었다.XSTREAM을 사용하여 XML을 ArrayList로 구문 분석

누군가가이 문제에 협조하면 도움을 받으십시오.

다음은 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

XStream은 어떻게 얻었습니까? "바이너리 배포판"을 다운로드하면 코어 XStream JAR뿐만 아니라 필요한 모든 종속성 JAR 파일이 포함될 것이므로 IDE 또는 빌드 시스템에 적합한 방식으로 응용 프로그램의 클래스 경로에 모두 추가해야합니다 사용. –

+0

예, 그게 문제였습니다. dendency list – AnZyuZya

답변

1

XStream을을 다른 라이브러리에 의존성이있다. 클래스 패스에 xmlpull 및 xpp3_min이 있는지 확인하십시오. Maven을 사용하면 다음과 같이 Maven을 사용할 수 있습니다.

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

Maven이 자동으로 모든 종속성을 해결합니다.

데이터 모델이나 XML에 오류가 있습니다.

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

는 어느 쪽이든 당신은 XML에 "ID" (대문자) 또는 id (소문자)에 데이터 모델에서 설정 확인하시기 바랍니다.

@XStreamAlias("camber") 
    private String camber; 

중 하나는 XML의 데이터 모델에 "camber_profile"에 별칭을 설정하거나 camber에 확인하시기 바랍니다.

마지막으로 당신에게하지 적어도 내 충고는 내부 목록의 참조를 반환 할 때 목록을 수정, 호출자가 임의의 수

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

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

을 사용하는 것입니다. 이것은 좋은 캡슐화를 위반합니다.

+0

에 전체 폴더를 추가해야하는 이유는 무엇입니까? XML 또는 ..의 시작 부분에? – AnZyuZya

+1

종속성은 xml 파일과 아무 관련이 없습니다. xstream-1.4.7.jar, xmlpull-1.1.3.1.jar (이상) 및 xpp3_min-1.1.4c.jar (또는 그 이상)를 Java 클래스 경로에 추가하십시오. – Vertex

+0

글쎄, 나는 그것들을 추가했다. 그리고 나는 XML의 구조를 간단히 (거의 두번) 구조화 한 후에 그것을 sudenly 작동한다. 감사. 당신의 대답이 성공했다고 생각합니다. – AnZyuZya