Boa noite galera,
Estou trabalhando em um projeto ao qual recebo de um Web Service um xml que faço o processo de desserialização utilizando o XStream. So que estou com um problema que, o parser de objetos não está funcionandoXStream.fromXml()
Gostaria de saber se existem alguem que possa me dar uma luz no fim do túnel!?
Segue o xml:
<?xml version="1.0" encoding="UTF-8"?>
<response-busca>
<documento path="path_ok" deletado="0"/>
<update>
<documento path="path_ok" deletado="1">
<indexacao>
<indice id="1" conteudo="um"/>
<indice id="2" conteudo="dois"/>
<indice id="3" conteudo="tres"/></indexacao>
</documento>
<documento path="path_ok" deletado="2">
<indexacao>
<indice id="4" conteudo="quatro"/>
<indice id="5" conteudo="cinco"/>
<indice id="6" conteudo="seis"/></indexacao>
</documento>
</update>
</response-busca>
Código executado:
XStream xstream = new XStream();
xstream.alias("response-busca", ResultadoResponse.class);
xstream.alias("documento", ResultadoDocumento.class);
xstream.aliasAttribute(ResultadoDocumento.class, "path", "path");
xstream.aliasAttribute(ResultadoDocumento.class, "data", "data");
xstream.aliasAttribute(ResultadoDocumento.class, "erroId", "erroId");
xstream.aliasAttribute(ResultadoDocumento.class, "erroMsg", "erroMsg");
xstream.aliasAttribute(ResultadoDocumento.class, "deletado", "deletado");
xstream.alias("update", ResultadoUpdate.class);
xstream.alias("indexacao", ResultadoIndexacao.class);
xstream.alias("indice", ResultadoIndice.class);
xstream.aliasAttribute(ResultadoIndice.class, "id", "id");
xstream.aliasAttribute(ResultadoIndice.class, "conteudo", "conteudo");
xstream.addImplicitCollection(ResultadoUpdate.class, "documento");
xstream.addImplicitCollection(ResultadoIndexacao.class, "indice");
O interessante é que este código de serialização abaixo funciona: (response é um bean ResultadoResponse, o elemento root)
String xml = xstream.toXML(response);
e já este de desserialização não funciona: (reparem que é o mesmo xml gerado pelo código acima, ou seja, o XStream não consegue desserializar o mesmo objeto que serializou, que estranho!):
ResultadoResponse resultadoResponse = (ResultadoResponse) xstream.fromXML(xml);
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Element indexacao of type br.com.sythex.webservice.bean.ResultadoIndexacao is not defined as field in type java.lang.Object
---- Debugging information ----
class : br.com.sythex.webservice.bean.ResultadoResponse
required-type : java.lang.Object
path : /response-busca/update/documento/indexacao
line number : 9
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.writeValueToImplicitCollection(AbstractReflectionConverter.java:283)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:236)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:225)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
at br.com.sythex.webservice.main.MainTest.main(MainTest.java:121)
at br.com.sythex.webservice.main.MainTest.main(MainTest.java:71)
Caso for necessário eu posto os Beans.
Alguem poderia me ajudar?
Estou fazendo algo de errado ?
Obrigado
[]'s