3

I'm trying to deserialize this XML file using XStream and I'm getting this error

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Authors : Authors
---- Debugging information ----
message             : Authors
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : Authors
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /ListOfDBook/DBook/Authors
class[1]            : com.test.books.ListOfDBook
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
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.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    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:1058)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:922)
    at com.test.booktest.BookImport.getBooks(BookImport.java:34)
    at com.test.booktest.Client.main(Client.java:6)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: Authors
    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)
    ... 16 more

With this XML

<ListOfDBook xmlns="Test.Books" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <DBook>
        <Authors>
            <DAuthor>
                <Name>Google</Name>
                <Url>http://google.com</Url>
            </DAuthor>
        </Authors>
    </DBook>
</ListOfDBook>

With these classes

//ListOfDBook.java
public class ListOfDBook {
    public ArrayList<DBook> DBook; 
}

//DBook.java
public class DBook {
    public ArrayList<DAuthor> Authors;
}

//DAuthor.java
public class DAuthor {
    String Name;
    String Url;
}

//BookImport.java
import com.test.books.*;
XStream xstream = new XStream(new DomDriver());
xstream.alias("ListOfDBook", ListOfDBook.class);
InputStream in = new FileInputStream("Books.xml");
ListOfDBook = (ListOfDBook)xstream.fromXML(in);

I recreated the classes from reading the XML file and changing the XML file is not an option. Am I doing something completely wrong?

2
  • I don't think your last line is right, there is no variable name. Commented Aug 17, 2013 at 18:29
  • Solved, check my second edit. Commented Aug 17, 2013 at 19:15

1 Answer 1

6

You are missing Authors?

//ListOfDBook.java
public class ListOfDBook {
    public ArrayList<DBook> DBook; 
}

//DBook.java
public class DBook {
    public ArrayList<Author> Authors;
}

//Author.java
public class Author {
    public ArrayList<DAuthor> DAuthors;
}

//DAuthor.java
public class DAuthor {
    String Name;
    String Url;
}

EDIT 2:

You need to use addImplicitCollection.

import java.util.*;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.*;
import com.thoughtworks.xstream.io.xml.*;
import java.io.*;

public class Test {

    public static void main(String[] args) throws Exception {

        XStream xstream = new XStream(new DomDriver());
        xstream.alias("ListOfDBook", ListOfDBook.class);
        xstream.alias("DBook", DBook.class);
        xstream.alias("Authors", Authors.class);
        xstream.alias("DAuthor", DAuthor.class);
        xstream.addImplicitCollection(Authors.class, "dauthors");
        xstream.addImplicitCollection(DBook.class, "authors");
        xstream.addImplicitCollection(ListOfDBook.class, "dbooks");
        InputStream in = new FileInputStream("Books.xml");
        ListOfDBook var = (ListOfDBook)xstream.fromXML(in);

    }

}

//ListOfDBook.java
class ListOfDBook {
    public List<DBook> dbooks = new LinkedList<DBook>(); 
}

//DBook.java
class DBook {
    public List<Authors> authors = new LinkedList<Authors>();
}

//Authors.java
class Authors {
    public List<DAuthor> dauthors = new LinkedList<DAuthor>();
}

//DAuthor.java
class DAuthor {
    public String Name;
    public String Url;
}
Sign up to request clarification or add additional context in comments.

7 Comments

You are correct I was missing Author. It then moves onto the Name element and have the same error. com.thoughtworks.xstream.converters.ConversionException: Name : Name I feel like I don't completely understand how I should be setting out the classes.
Try aliasing the custom classes, check this link: xstream.codehaus.org/tutorial.html Also, try lowercase variable names.
Creating the Class 'Name' fixes that and the same happens for the next element, 'Url'. I can't be making a new class for every different element when the element should be part of the parent element right?
No You do not need a custom class for the Strings. Try using lowercase variable names first.
Same error with lowercase. Tried adding this to DAuthor class public DAuthor(String Name, String URL) { this.name = Name; this.url = URL; } also did nothing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.