XStream Objekte unmarshallen

Hi,
ich versuche ein XML-Parser mit der XStream Bibliothek zu bauen. Was ich über die Bibliothek gehört habe kling auch gut, das Marshallen funktioniert in einem Test auch schon. Das Unmarschallen komischerweise nicht. Gehe ich da verkehrt ran? Über einen Hinweis würde ich mich freuen:)

Marshalling Test

    public void serializeTest(){
        
        System.out.println(xStream.toXML(getSampleOutput()));
    }

private Output getSampleOutput(){
        Output eo=new Output();
        IoFormat ef=new IoFormat();
        ef.setName("FormatName");
        eo.setFormat(ef);
        IoSize es=new IoSize();
        es.setBytes(123L);
        eo.setSize(es);
        IoPages eop=new IoPages();
        eop.setCount(2);
        eo.setPages(eop);
        return eo;
    }

Unmarshallig Test


private final XStream xStream;

    public ParserTest() {
        xStream = new XStream(new StaxDriver());
        xStream.autodetectAnnotations(true);
    }
...

@Test
    public void outputParsingTest() throws IOException {
        File file = new File("../importer/test/output.log");
        System.out.println(file.getCanonicalPath() + " " + file.exists());
        assertTrue(file.getCanonicalPath() + " have to exist", file.exists());
        try {
            Output eo = (Output) xStream.fromXML(file);
            System.out.println(eo);
            assertNotNull("EntryOutput instance not null", eo);
            assertTrue("eo instanceof EntryOutput", eo instanceof Output);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Hier noch die Fehlermeldung:
com.thoughtworks.xstream.mapper.CannotResolveClassException: output 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.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:1154) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1096)

Hi,

wie sieht die XML aus und wie sieht die OUTPUT Klasse aus?

Gruß,
Martin

Hier ist die XML-Die Output und deren Unterklassen
[XML]



[/XML]

public class Output {
    
    @XStreamAlias("size")
    private IoSize size;
    
    @XStreamAlias("format")
    private IoFormat format;
    
    @XStreamAlias("pages")
    private IoPages pages;

    public IoSize getSize() {
        return size;
    }

    public void setSize(IoSize size) {
        this.size = size;
    }

    public IoFormat getFormat() {
        return format;
    }

    public void setFormat(IoFormat format) {
        this.format = format;
    }

    public IoPages getPages() {
        return pages;
    }

    public void setPages(IoPages pages) {
        this.pages = pages;
    }

}```

```public class IoFormat {

        @XStreamAlias("name")
        @XStreamAsAttribute
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }```

```public class IoPages {

    @XStreamAlias("count")
    @XStreamAsAttribute
    Integer count;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }
}```

```public class IoSize {

    @XStreamAlias("bytes")
    @XStreamAsAttribute
    private Long bytes;

    public Long getBytes() {
        return bytes;
    }

    public void setBytes(Long bytes) {
        this.bytes = bytes;
    }

}```

Hi,

leider ist so, dass auto detect annotations nur für die von XStream verarbeiteten (bekannten) Klassen durchgeführt wird. Im speziellen kann die Klasse output nicht geladen werden, weil XStream die Annotationen nicht gescannt hat.

Entweder man verzichtet auf diese Annotation oder man verwendet “xstream.processAnnotation()”, um die Klasse von XStream verarbeiten zu lassen.

Es hat mich ein wenig gewundert, dass das so ist, obwohl ich es iind ieser Form von meinem Projekt verwende. Ich habe aber keine XStreamAlias Annotationen, so dass der volle Klassenname serialisiert wird.

I.d.r. sollte das Aussehen der XML für das Programm egal sein.

Gruß,
Martin

Danke mit

...
xStream.processAnnotations(Output.class);
        xStream.processAnnotations(IoFormat.class);
        xStream.processAnnotations(IoPages.class);
        xStream.processAnnotations(IoSize.class);
...

funktioniert es!