如何使用XStream将内容解析为Java Object

时间:2014-06-19 08:29:36

标签: java xml parsing xstream

这是我的Xml内容,我还准备了一些Java Object进行解析。现在我真的没有任何线索,请帮我解决这个问题。总结一下,我想将xml内容映射解析为对象列表,每个对象包含另一个项列表。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:getSimpleBoJuniorResponse xmlns:ns2="http://service.viettel.com/">
         <return>
            <list>
               <date>2014-06-19T14:29:51.592+07:00</date>
               <id>1</id>
               <name>1</name>
            </list>
         </return>
         <return>
            <list>
               <date>2014-06-19T14:29:51.592+07:00</date>
               <id>1</id>
               <name>1</name>
            </list>
         </return>
      </ns2:getSimpleBoJuniorResponse>
   </soap:Body>
</soap:Envelope>

Class SimpleBo

public class SimpleBo implements Serializable{
    String name;
    Date date;
    long id;

}

Class SimpleBoJunior

public class SimpleBoJunior implements Serializable{
    String name;
    private List<SimpleBo> list = new ArrayList<SimpleBo>();

}

Class SimpleJuniorList

public class SimpleBoJuniorList {

    List<SimpleBoJunior> simpleBoJuniors = new ArrayList<SimpleBoJunior>();


}

我尝试添加一些别名或addImplicitCollection,就像那样

    XStream xStream = new XStream();
    xStream.alias("ns2:getSimpleBoJuniorResponse",SimpleBoJuniorList.class);
    xStream.alias("return",SimpleBoJunior.class);
    xStream.addImplicitCollection(SimpleBoJuniorList.class, "simpleBoJuniors");
    String[] formats = { "yyyy-MM-dd G HH:mm:ss.S z" };
    xStream.registerConverter(new DateConverter("yyyy-MM-dd", formats));

Howerver,我总是得到错误,比如

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: id : id
---- Debugging information ----
message             : id
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : id
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /ns2:getSimpleBoJuniorResponse/return/list/id
line number         : 5
class[1]            : com.viettel.cbs.order.dto.SimpleBoJunior
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2]            : com.viettel.cbs.order.dto.SimpleBoJuniorList
version             : 1.4.5
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:464)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:396)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:247)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:367)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:247)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1156)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1140)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1011)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1002)
    at com.viettel.webservice.handler.Test.main(Test.java:143)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: id
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:98)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:91)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:85)
    at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:80)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 22 more

0 个答案:

没有答案