Interesting example Stephen, I'll try and look into it a bit more when I have chance to and get back to you on it. The deriveByClass code is basically at the top of the private Marshaller#marshal() method so it really deals with elements only. I'll see what it would take to tweak the attribute processing code to handle the deriveByClass.
--Keith
Stephen Bash wrote:
Everyone-
I recently ran across a situation where it would be beneficial to use auto-naming for attributes (rather than elements), and I was wondering if this had ever come up before (I didn't find anything in the list archives).
As motivation, consider the following example:
public class DataContainer { private Map data;
public Map getData() { return data; }
public void setData( Map data ) { this.data = data; } }
The following code instantiates the above class and then marshals it out (error handling removed):
public class DataToXML { public static void main( String[] args ) { HashMap mapData = new HashMap(); mapData.put( "Boolean1", new Boolean( true ) ); mapData.put( "String1", "Test1" ); mapData.put( "Boolean2", new Boolean( false ) ); mapData.put( "String2", "Test2" );
DataContainer myData = new DataContainer(); myData.setData( mapData );
Mapping myMap = new Mapping(); myMap.loadMapping( "dataContainerMap.xml" ); Marshaller m1 = new Marshaller( new FileWriter( "myData.xml") ); m1.setMapping( myMap ); m1.marshal( myData ); } }
Following is what I would like (note: this probably also involves the bug with deriveByClass and the location attribute):
<DataContainer> <DataMap> <DataEntry dataKey="Boolean1" boolean="true" /> <DataEntry dataKey="String1" string="Test1" /> <DataEntry dataKey="Boolean2" boolean="false" /> <DataEntry dataKey="String2" string="Test2" /> </DataMap> </DataContainer>
Currently, when I specify node="attribute" and auto-naming="deriveByClass" I get a NPE as follows (note: in this case I am not using the location attribute as it is a known bug):
java.lang.NullPointerException
at org.apache.xml.serialize.XMLSerializer.startElement(XMLSerializer.java:421)
at org.exolab.castor.xml.util.DocumentHandlerAdapter.startElement(DocumentHandlerAdapter.java:197)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1327) at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1543) at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:782) at DataToXML.main(DataToXML.java:51) Exception in thread "main"
The complete mapping file is below:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://castor.exolab.org/mapping.dtd">
<mapping> <class name="DataContainer"> <map-to xml="DataContainer" /> <field name="data" collection="map"> <bind-xml name="DataEntry"> <class name="org.exolab.castor.mapping.MapItem"> <field name="key" type="string"> <bind-xml name="dataKey" node="attribute"/> </field> <field name="value"> <bind-xml auto-naming="deriveByClass" node="attribute" /> </field> </class> </bind-xml> </field> </class> </mapping>
Any comments or suggestions would be greatly appreciated. Thanks for you time.
Stephen
----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev