Here is the second approach I tried. Note that this option uses addChild() instead of setChildren().
On the lastest cut of CVS, I'm trying to marshal/unmarshal a collection, like in this post: http://www.mail-archive.com/[email protected]/msg07873.html Marshalling works great. When I unmarshall, I get this exception. Thanks in advance (again) for looking at this. See bottom of email for code and mapping file. [java] org.xml.sax.SAXException: unable to add 'Child' to <Parent> due to the following exception: [java] >>>--- Begin Exception ---<<< [java] java.lang.UnsupportedOperationException [java] at java.util.AbstractCollection.add(AbstractCollection.java:212) [java] at org.exolab.castor.mapping.loader.J2CollectionHandlers$2.add(J2Collection Handlers.java:124) [java] at org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(FieldHandlerI mpl.java:555) [java] at org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java: 705) [java] at org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1392) [java] at org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValid ator.java:1550) [java] at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatc h(XMLDocumentScanner.java:1149) [java] at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScan ner.java:381) [java] at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098) [java] at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:485) [java] at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:417) [java] at HashTest.main(HashTest.java:68) [java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav a:39) [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor Impl.java:25) [java] at java.lang.reflect.Method.invoke(Method.java:324) [java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:208) [java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:150) [java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:414) [java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:163) [java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108) [java] at org.apache.tools.ant.Task.perform(Task.java:317) [java] at org.apache.tools.ant.Target.execute(Target.java:309) [java] at org.apache.tools.ant.Target.performTasks(Target.java:334) [java] at org.apache.tools.ant.Project.executeTarget(Project.java:1306) [java] at org.apache.tools.ant.Project.executeTargets(Project.java:1250) [java] at org.apache.tools.ant.Main.runBuild(Main.java:606) [java] at org.apache.tools.ant.Main.start(Main.java:195) [java] at org.apache.tools.ant.Main.main(Main.java:234) [java] >>>---- End Exception ----<<< [java] {file: ; line: 2; column: 163} ////////////////////////// Parent.java ////////////////////// import java.util.Collection; import java.util.HashMap; public class Parent { HashMap _children = new HashMap(); public void addChild(Child c) { _children.put(c.getKey(), c); } public Child getChild(Object key) { return (Child) _children.get(key); } public Collection getChildren() { return _children.values(); } } ///////////////////////// Child.java ///////////////////// public class Child { String firstName = null; Object key = null; public Child(){} public String getFirstName() { return firstName; } public void setFirstName(String strName) { firstName = strName; } public void setKey(Object objKey) { key = objKey; } public Object getKey() { return key; } public String toString() { return firstName; } } ///////////////// HashTest.java ////////////////////////////// import java.util.HashMap; import java.util.Iterator; import java.util.Collection; import java.io.StringWriter; import java.io.StringReader; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.Marshaller; import java.io.IOException; import java.io.FileReader; import java.io.OutputStreamWriter; import org.xml.sax.InputSource; public class HashTest { public static void main(String[] args) { Parent p = new Parent(); Child c = new Child(); c.setFirstName("Betty"); c.setKey("betty_key"); p.addChild( c); c = new Child(); c.setFirstName("Wilma"); c.setKey("wilma_key"); p.addChild( c ); c = new Child(); c.setFirstName("Fred"); c.setKey("fred_key"); p.addChild( c ); c = new Child(); c.setFirstName("Barney"); c.setKey("barney_key"); p.addChild( c ); testParent(p); Mapping mapping = new Mapping(); try { // 1. Load the mapping information from the file mapping.loadMapping( "mapping.xml" ); StringWriter sw = new StringWriter(); // 4. marshal the data with the total price back and print the XML in the console Marshaller marshaller = new Marshaller(sw); marshaller.setMapping(mapping); marshaller.marshal(p); System.out.println("Here is the data [" + sw.toString() + "]"); StringReader sr = new StringReader(sw.toString()); // 2. Unmarshal the data Unmarshaller unmar = new Unmarshaller(mapping); /////////////////////////////////////////////////// /////////// /////////// this throws an exception /////////// Parent newParent = (Parent)unmar.unmarshal(sr); } catch (Exception e) { System.out.println(e); } } public static void testParent(Parent p) { System.out.println("key: Fred_key child: [" + p.getChild("fred_key").toString() + "]"); Collection kids = p.getChildren(); for(Iterator itr = kids.iterator(); itr.hasNext();) { Child kid = (Child)itr.next(); System.out.println("Here is a child [" + kid.toString() + "] key [" + kid.getKey() + "]"); } } } ----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
