Xtream com.thoughtworks.xstream.mapper.CannotResolveClassException

时间:2015-11-25 14:59:59

标签: java xml xstream

我正在尝试XStream。但是当我尝试解析我的xml文件时,我得到了这个异常。

Xml数据在下面:类别>类别>儿童>类别

<SuccessResponse>
 <Head>
      <RequestId></RequestId>
      <RequestAction>GetCategoryTree</RequestAction>
      <ResponseType>Categories</ResponseType>
      <Timestamp>2015-11-25T15:28:09+0800</Timestamp>
 </Head>
 <Body>
      <Categories>
           <Category>
            <Name>Chinese Books</Name>
            <CategoryId>3460</CategoryId>
            <GlobalIdentifier>chinese_books</GlobalIdentifier>
            <Children>
              <Category>
                <Name>Literature</Name>
                <CategoryId>3461</CategoryId>
                <GlobalIdentifier>literature2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Lifestyle Books</Name>
                <CategoryId>3462</CategoryId>
                <GlobalIdentifier>lifestyle_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Teens Books</Name>
                <CategoryId>3463</CategoryId>
                <GlobalIdentifier>teens_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Biography Books</Name>
                <CategoryId>3464</CategoryId>
                <GlobalIdentifier>biography_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Business Books</Name>
                <CategoryId>3465</CategoryId>
                <GlobalIdentifier>business_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Calendars</Name>
                <CategoryId>3466</CategoryId>
                <GlobalIdentifier>calendars3</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Babies' Books</Name>
                <CategoryId>3467</CategoryId>
                <GlobalIdentifier>babies_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Toddlers' Books</Name>
                <CategoryId>3468</CategoryId>
                <GlobalIdentifier>toddlers_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Parent's Books</Name>
                <CategoryId>3469</CategoryId>
                <GlobalIdentifier>parents_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Children's Books</Name>
                <CategoryId>3470</CategoryId>
                <GlobalIdentifier>childrens_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Religion Books</Name>
                <CategoryId>3471</CategoryId>
                <GlobalIdentifier>religion_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Comic Books</Name>
                <CategoryId>3472</CategoryId>
                <GlobalIdentifier>comic_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Technology Books</Name>
                <CategoryId>3473</CategoryId>
                <GlobalIdentifier>technology_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Cooking Books</Name>
                <CategoryId>3474</CategoryId>
                <GlobalIdentifier>cooking_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Pre Sell Books</Name>
                <CategoryId>3476</CategoryId>
                <GlobalIdentifier>pre_sell_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Education Books</Name>
                <CategoryId>3477</CategoryId>
                <GlobalIdentifier>education_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Fitness Books</Name>
                <CategoryId>3478</CategoryId>
                <GlobalIdentifier>fitness_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>History Books</Name>
                <CategoryId>3479</CategoryId>
                <GlobalIdentifier>history_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Entertainment Books</Name>
                <CategoryId>3480</CategoryId>
                <GlobalIdentifier>entertainment_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Law Books</Name>
                <CategoryId>3481</CategoryId>
                <GlobalIdentifier>law_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Art Books</Name>
                <CategoryId>3482</CategoryId>
                <GlobalIdentifier>art_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Medical Books</Name>
                <CategoryId>3483</CategoryId>
                <GlobalIdentifier>medical_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Thrillers</Name>
                <CategoryId>3484</CategoryId>
                <GlobalIdentifier>thrillers2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Relationship Books</Name>
                <CategoryId>3485</CategoryId>
                <GlobalIdentifier>relationship_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Social Books</Name>
                <CategoryId>3486</CategoryId>
                <GlobalIdentifier>social_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Professional Books</Name>
                <CategoryId>3487</CategoryId>
                <GlobalIdentifier>professional_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Romance</Name>
                <CategoryId>3488</CategoryId>
                <GlobalIdentifier>romance3</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Science Books</Name>
                <CategoryId>3489</CategoryId>
                <GlobalIdentifier>science_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Science Fiction Books</Name>
                <CategoryId>3490</CategoryId>
                <GlobalIdentifier>science_fiction_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Self Help</Name>
                <CategoryId>3491</CategoryId>
                <GlobalIdentifier>self_help2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Sport Books</Name>
                <CategoryId>3492</CategoryId>
                <GlobalIdentifier>sport_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Travel Books</Name>
                <CategoryId>3493</CategoryId>
                <GlobalIdentifier>travel_books2</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Study Aids &amp; Exam Preparations</Name>
                <CategoryId>8453</CategoryId>
                <GlobalIdentifier>study_aids_exam_preparations</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Language, Linguistics &amp; Writing</Name>
                <CategoryId>8455</CategoryId>
                <GlobalIdentifier>language_linguistics_writing</GlobalIdentifier>
                <Children>
                  <Category>
                    <Name>Language Learning &amp; Teaching</Name>
                    <CategoryId>8458</CategoryId>
                    <GlobalIdentifier>language_learning_teaching</GlobalIdentifier>
                    <Children/>
                  </Category>
                  <Category>
                    <Name>Linguistics</Name>
                    <CategoryId>8459</CategoryId>
                    <GlobalIdentifier>linguistics</GlobalIdentifier>
                    <Children/>
                  </Category>
                  <Category>
                    <Name>Grammar</Name>
                    <CategoryId>8460</CategoryId>
                    <GlobalIdentifier>grammar</GlobalIdentifier>
                    <Children/>
                  </Category>
                  <Category>
                    <Name>Dictionaries</Name>
                    <CategoryId>8461</CategoryId>
                    <GlobalIdentifier>dictionaries</GlobalIdentifier>
                    <Children/>
                  </Category>
                </Children>
              </Category>
              <Category>
                <Name>Text books</Name>
                <CategoryId>8462</CategoryId>
                <GlobalIdentifier>text_books</GlobalIdentifier>
                <Children>
                  <Category>
                    <Name>High School</Name>
                    <CategoryId>8463</CategoryId>
                    <GlobalIdentifier>high_school1</GlobalIdentifier>
                    <Children/>
                  </Category>
                  <Category>
                    <Name>University</Name>
                    <CategoryId>8464</CategoryId>
                    <GlobalIdentifier>university1</GlobalIdentifier>
                    <Children/>
                  </Category>
                </Children>
              </Category>
            </Children>
          </Category>
          <Category>
            <Name>Calendars</Name>
            <CategoryId>8465</CategoryId>
            <GlobalIdentifier>calendars</GlobalIdentifier>
            <Children>
              <Category>
                <Name>Wall calendars</Name>
                <CategoryId>8466</CategoryId>
                <GlobalIdentifier>wall_calendars</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Mini wall calendars</Name>
                <CategoryId>8467</CategoryId>
                <GlobalIdentifier>mini_wall_calendars</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Box calendars</Name>
                <CategoryId>8468</CategoryId>
                <GlobalIdentifier>box_calendars</GlobalIdentifier>
                <Children/>
              </Category>
              <Category>
                <Name>Planners</Name>
                <CategoryId>8469</CategoryId>
                <GlobalIdentifier>planners</GlobalIdentifier>
                <Children/>
              </Category>
            </Children>
          </Category>
      </Categories>
 </Body>

分类对象

@XStreamAlias("Categories")
public class Categories {
    private List<Category> Category;
 }

类别对象

@XStreamAlias("Category")
public class Category {

private String Name;
private String CategoryId;
private String GlobalIdentifier;
private Children Children;
}

儿童对象

@XStreamAlias("Children")
public class Children {
    private List<Category> Category;
}

解析:

XStream xs = new XStream();
xs.autodetectAnnotations(true);

xs.alias("Categories", Categories.class);
xs.alias("SuccessResponse", CategorySuccessResponse.class);

CategorySuccessResponse successResponse = (CategorySuccessResponse) xs
            .fromXML(response);

错误:

---- Debugging information ----
message             : Name
cause-exception     :              
com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : Name
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      :     
com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /SuccessResponse/Body/Categories/Category/Name
line number         : 12
version             : 1.4.8

这个错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,无论你在哪里定义List数据,都要尝试@XStreamImplicit

@XStreamAlias("Categories")
public class Categories {
     @XStreamImplicit
     private List<Category> Category;
 }