CannotResolveClassException during object unmarshal

View: New views
3 Messages — Rating Filter:   Alert me  

CannotResolveClassException during object unmarshal

by ale.marino78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

CannotResolveClassException during object unmarshal

I get the following exception while trying to unmarshal an object.

$ java -classpath .:junit.jar:xstream/1.3/xstream.jar org.junit.runner.JUnitCore  it.alessandro.MarshalUnmarshalXmlTestCase
JUnit version 4.5
serialized xml: **<operation_result>blah blah ...</operation_result>**
Time: 0.149
There was 1 failure:
1) testUnmarshalXmlTagWithUnderscore(it.alessandro.MarshalUnmarshalXmlTestCase)
com.thoughtworks.xstream.mapper.CannotResolveClassException: <operation_result>blah blah ...</operation_result> : <operation_result>blah blah ...</operation_result>
        at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:62)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:71)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:86)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:87)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
        at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:52)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:138)
        at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:931)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:907)
        at it.alessandro.MarshalUnmarshalXmlTestCase.testUnmarshalXmlTagWithUnderscore(MarshalUnmarshalXmlTestCase.java:28)

The code of the test case is this:

public class MarshalUnmarshalXmlTestCase extends TestCase {
        private static final XmlFriendlyReplacer XML_FRIENDLY_REPLACER = new XmlFriendlyReplacer("§", "_");

        public void testUnmarshalXmlTagWithUnderscore() {
                String operation = "blah blah ...";

                StringWriter s = new StringWriter();
                XStream xs = new XStream();
                xs.alias("operation_result", String.class);
                xs.marshal(operation, (HierarchicalStreamWriter) new PrettyPrintWriter(s, XML_FRIENDLY_REPLACER));
                String xml = s.toString();
                System.out.println("serialized xml: **" + xml+"**");

                String unmarshalledOperation = (String) xs.unmarshal((HierarchicalStreamReader) new XppDomReader(new Xpp3Dom(xml), XML_FRIENDLY_REPLACER));

                assertEquals(unmarshalledOperation, operation);
        }
}

I don't understand why Xstream is throwing this exception since I'm using just a String class. Is it possible that the XmlFriendlyReplacer could cause this exception when unmarshalling?

Thanks and regards,
Alessandro
  


Re: CannotResolveClassException during object unmarshal

by Jörg Schaible-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alessandro,

ale.marino78@... wrote:

> I get the following exception while trying to unmarshal an object.
>
> $ java -classpath .:junit.jar:xstream/1.3/xstream.jar
> org.junit.runner.JUnitCore  it.alessandro.MarshalUnmarshalXmlTestCase
> JUnit version 4.5 serialized xml: **<operation_result>blah blah
> ...</operation_result>** Time: 0.149
> There was 1 failure:
> 1)
>
testUnmarshalXmlTagWithUnderscore(it.alessandro.MarshalUnmarshalXmlTestCase)
> com.thoughtworks.xstream.mapper.CannotResolveClassException:
> <operation_result>blah blah ...</operation_result> :
> <operation_result>blah blah ...</operation_result>
>         at
>        
com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:62)
>         at

[snip]

>
> The code of the test case is this:
>
> public class MarshalUnmarshalXmlTestCase extends TestCase {
>         private static final XmlFriendlyReplacer XML_FRIENDLY_REPLACER =
>         new XmlFriendlyReplacer("§", "_");
>
>         public void testUnmarshalXmlTagWithUnderscore() {
>                 String operation = "blah blah ...";
>
>                 StringWriter s = new StringWriter();
>                 XStream xs = new XStream();
>                 xs.alias("operation_result", String.class);
>                 xs.marshal(operation, (HierarchicalStreamWriter) new
>                 PrettyPrintWriter(s, XML_FRIENDLY_REPLACER)); String xml =
>                 s.toString(); System.out.println("serialized xml: **" +
>                 xml+"**");
>
>                 String unmarshalledOperation = (String)
>                 xs.unmarshal((HierarchicalStreamReader) new
>                 XppDomReader(new Xpp3Dom(xml), XML_FRIENDLY_REPLACER));
>
>                 assertEquals(unmarshalledOperation, operation);
>         }
> }
>
> I don't understand why Xstream is throwing this exception since I'm using
> just a String class. Is it possible that the XmlFriendlyReplacer could
> cause this exception when unmarshalling?

No, you set the complete XML string as tag name of the DOM element. That's
exactly what the error says, XStream cannot find a class (or alias) with
the name "<operation_result>blah blah ...</operation_result>". If you want
to parse XML into a Xpp3Dom you have to use the Xpp3DomBuilder.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



R: [xstream-user] Re: CannotResolveClassException during object unmarshal

by ale.marino78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Changing the following line of the test case:

String unmarshalledOperation = (String) xs.unmarshal((HierarchicalStreamReader) new XppDomReader(new Xpp3Dom(xml), XML_FRIENDLY_REPLACER));

with this one, solved the problem:

String unmarshalledOperation = (String) xs.unmarshal(new XppDomDriver(XML_FRIENDLY_REPLACER).createReader(new StringReader(xml)));

Thanks,
Alessandro


-----Messaggio originale-----
Da: news per conto di Jörg Schaible
Inviato: gio 15/10/2009 23.30
A: user@...
Oggetto: [xstream-user]  Re: CannotResolveClassException during object unmarshal
 
Hi Alessandro,

ale.marino78@... wrote:

> I get the following exception while trying to unmarshal an object.
>
> $ java -classpath .:junit.jar:xstream/1.3/xstream.jar
> org.junit.runner.JUnitCore  it.alessandro.MarshalUnmarshalXmlTestCase
> JUnit version 4.5 serialized xml: **<operation_result>blah blah
> ...</operation_result>** Time: 0.149
> There was 1 failure:
> 1)
>
testUnmarshalXmlTagWithUnderscore(it.alessandro.MarshalUnmarshalXmlTestCase)
> com.thoughtworks.xstream.mapper.CannotResolveClassException:
> <operation_result>blah blah ...</operation_result> :
> <operation_result>blah blah ...</operation_result>
>         at
>        
com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:62)
>         at

[snip]

>
> The code of the test case is this:
>
> public class MarshalUnmarshalXmlTestCase extends TestCase {
>         private static final XmlFriendlyReplacer XML_FRIENDLY_REPLACER =
>         new XmlFriendlyReplacer("§", "_");
>
>         public void testUnmarshalXmlTagWithUnderscore() {
>                 String operation = "blah blah ...";
>
>                 StringWriter s = new StringWriter();
>                 XStream xs = new XStream();
>                 xs.alias("operation_result", String.class);
>                 xs.marshal(operation, (HierarchicalStreamWriter) new
>                 PrettyPrintWriter(s, XML_FRIENDLY_REPLACER)); String xml =
>                 s.toString(); System.out.println("serialized xml: **" +
>                 xml+"**");
>
>                 String unmarshalledOperation = (String)
>                 xs.unmarshal((HierarchicalStreamReader) new
>                 XppDomReader(new Xpp3Dom(xml), XML_FRIENDLY_REPLACER));
>
>                 assertEquals(unmarshalledOperation, operation);
>         }
> }
>
> I don't understand why Xstream is throwing this exception since I'm using
> just a String class. Is it possible that the XmlFriendlyReplacer could
> cause this exception when unmarshalling?
No, you set the complete XML string as tag name of the DOM element. That's
exactly what the error says, XStream cannot find a class (or alias) with
the name "<operation_result>blah blah ...</operation_result>". If you want
to parse XML into a Xpp3Dom you have to use the Xpp3DomBuilder.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

winmail.dat (5K) Download Attachment