xml跳过根元素

时间:2015-05-11 21:17:00

标签: java xml parsing xstream

所以我在字符串中有以下xml格式:

<applist>
    <app stuff="blah"/>
    <app stuff="blah"/>
</applist>

我想要一个app

列表

所以我这样做:

XStream xstream = new XStream();
xstream.alias("applist", List.class);
List<Application> list = (List<Application>) xstream.fromXML(xml); //xml is what is above

然后这是我的实体对象:

@XmlRootElement(name = "app")
public class Application{

    private String stuff;

    public String getStuff() {
        return stuff;
    }
    public void setStuff(String stuff) {
        this.stuff = stuff;
    }
}

我认为xstream.alias("applist", List.class);应该忽略根元素......在这种情况下applist。但是,这不起作用,我得到了这个例外。

Exception in thread "main"   com.thoughtworks.xstream.converters.ConversionException: app : app
---- Debugging information ----
message             : app
cause-exception     :   com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : app
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      :  com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /applist/app
line number         : 4
version             : null
-------------------------------
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.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:1058)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:913)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:904)
at TestStuff.main(TestStuff.java:19)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: app
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
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:79)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 10 more

那我为什么得到这个例外?我认为它会解析得非常好。

更新

我添加了xstream.alias("app", Application.class);,它现在将对象放入列表中。但是,stuff的值为null

1 个答案:

答案 0 :(得分:1)

我使用XStream实现了一个系统,我没有找到重大问题,它是一个很好的库来处理XML:)

我相信你不需要注册&#34; List.class&#34;但只是你的&#34; Application.class&#34;。

我会做以下事情:

XStream xstream = new XStream(new StaxDriver());
xstream.alias("app", Application.class);
xStream.useAttributeFor(Application.class, "stuff");
List<Application> list = (List<Application>) xstream.fromXML(xml); 

如果这对你有用,请告诉我。快乐的编码!!!