如何在没有ROOT节点

时间:2015-07-31 05:53:13

标签: java xml xstream

我的XML响应如下。我想跳过GIA(*其他XML的所有根节点)节点并使用List<Referenza>获取XStream是否真的可能?

XML

<?xml version="1.0" encoding="UTF-8"?>
<GIA>
  <Referenza>
    <GiaDat>20150727</GiaDat>
    <GiaMag>01</GiaMag>
    <GiaArt>SK01-0500</GiaArt>
    <GiaDes>KATSUOBUSHI POWDER 500G</GiaDes>
    <GiaLot></GiaLot>
    <GiaStm>CF</GiaStm>
    <GiaQta>88</GiaQta>
    <GiaImp>0</GiaImp>
    <GiaCon>88</GiaCon>
  </Referenza>
  <Referenza>
    <GiaDat>20150727</GiaDat>
    <GiaMag>01</GiaMag>
    <GiaArt>SK02-0500</GiaArt>
    <GiaDes>Katsuobushi Flakes 500g</GiaDes>
    <GiaLot></GiaLot>
    <GiaStm>CF</GiaStm>
    <GiaQta>130</GiaQta>
    <GiaImp>0</GiaImp>
    <GiaCon>130</GiaCon>
  </Referenza>
  ...
  ...
</GIA>

Referenza.java

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Referenza")
public class Referenza {
    private String GiaCon;
    private String GiaLot;
    private String GiaArt;
    private String GiaDes;
    private String GiaImp;
    private String GiaDat;
    private String GiaQta;
    private String GiaMag;
    private String GiaStm;

    // Getter-Setter
}

Test.java

String strData = strBatch.toString(); //XML Data

XStream xstream = new XStream(new DomDriver());
xstream.alias("Referenza", Referenza.class);
xstream.autodetectAnnotations(true);

/* I want the List of Referenza */
//List<Referenza> listReferenza = (List<Referenza>) xstream.fromXML(strData, Referenza.class);

如果我执行上面的代码,那么它会引发异常......

Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: GIA
    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.SecurityMapper.realClass(SecurityMapper.java:71)
    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:47)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1206)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1190)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1120)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1108)
    at com.org.openxcell.channel.orderhive.ftp.response.Test.main(Test.java:37)

感谢Jon Skeet以下代码为我工作。

XStream xstream = new XStream(new DomDriver());
xstream.alias("GIA", List.class);
xstream.alias("Referenza", Referenza.class);
xstream.autodetectAnnotations(true);
List<Object> list=  (List<Object>) xstream.fromXML(strData, new Referenza());

0 个答案:

没有答案