Hello there!
I have a problem unmarshalling arrays of java interfaces. Look at the
following example:
Class1:
public class ArrayTest implements ArrayTestInt {
private String[] strings = null;
}
Class 2:
public class ArrayTest2 implements ArrayTestInt {
private String[] strings = null;
}
Class3:
public class ArrayTest3 {
private ArrayTestInt[] tests;
public ArrayTestInt[] getTests() {
return tests;
}
public void setTests(ArrayTestInt[] tests) {
this.tests = tests;
}
}
Interface1:
public interface ArrayTestInt {
}
TesterClass:
import java.io.Serializable;
public class ArrayTester {
public static void main(String[] args) {
ArrayTest test = new ArrayTest();
ArrayTest2 test2 = new ArrayTest2();
ArrayTest3 test3 = new ArrayTest3();
test3.setTests(new ArrayTestInt[] { test, test2 });
String xmlData = (String)
XMLUtilities.convertObjectToXML(test3);
System.out.println(xmlData);
ArrayTest3 test4 =
(ArrayTest3) XMLUtilities.convertXMLToObject(
(Serializable) xmlData,
ArrayTest3.class);
String xmlData2 = (String)
XMLUtilities.convertObjectToXML(test4);
System.out.println(xmlData2);
}
}
UtilityClass:
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringReader;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
public class XMLUtilities {
public static Serializable convertObjectToXML(Object data) {
String returnValue = null;
if(data != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream
();
PrintWriter writer = new PrintWriter(stream);
try {
Marshaller.marshal(data, writer);
returnValue = stream.toString();
} catch(Exception e) {
//failed to convert data -> returnvalue = null
e.printStackTrace();
returnValue = null;
}
}
return ((Serializable)returnValue);
}
public static Object convertXMLToObject(Serializable data, Class
resultClass) {
Object returnValue = null;
if(data != null && resultClass != null) {
StringReader reader = new StringReader
((String)data.toString());
try {
returnValue = (Object)
Unmarshaller.unmarshal(resultClass, reader);
} catch(Exception e) {
//failed to convert data -> returnvalue = null
e.printStackTrace();
returnValue = null;
}
}
return (returnValue);
}
}
I convert an object with an array of objects of type ArrayTestInt into xml
and get the following output:
<?xml version="1.0"?>
<array-test3><tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:ArrayTest"/><tests xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:ArrayTest2
"/></array-test3>
When I convert this back into an object I get the following exception:
org.xml.sax.SAXException: unable to add 'tests' to <array-test3> due to the
following exception:
>>>--- Begin Exception ---<<<
java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at
org.exolab.castor.mapping.loader.J1CollectionHandlers$1.add(J1CollectionHandlers.java:109)
at
org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(FieldHandlerImpl.java:513)
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:526)
at
org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1392)
at
org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java:1180)
at
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1861)
at
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1234)
at
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:338)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:270)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:391)
at XMLUtilities.convertXMLToObject(XMLUtilities.java:96)
at ArrayTester.main(ArrayTester.java:16)
>>>---- End Exception ----<<<
at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:557)
at
org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1392)
at
org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java:1180)
at
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1861)
at
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1234)
at
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:338)null
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:270)
at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:391)
at XMLUtilities.convertXMLToObject(XMLUtilities.java:96)
at ArrayTester.main(ArrayTester.java:16)
I don't want to use a mapping file because my real objects are a bit
complicated. Could you tell me what I should do to solve my problem?
I searched the mail archive and found a similiar problem posted in february
2001 but I didn't find any answer. Please help!
Thanks in advance
stefan
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev