PDA

View Full Version : HowTo: Map XML Attributes


Blaukool
Jul 26th, 2007, 10:49 AM
Hello,

in my current project we need to build a batch program that reads XML data and transfers that data in to our database. After a look at the xmlJob example its easy to map a simple xml file. But we also need to map some XML attributes not only tags. Neither the sample nor the documentation I could find provided any hint at how to do it.
Only thing I found was the framework class AttributeAlias, which seams to provide the feature. But I didn't find any documentation on how to use it.

Does anybody have a sample of how to map attributes?

Thanks in advance
Marco

Dave Syer
Jul 27th, 2007, 02:17 AM
The current implementation of the XML mapping uses XStream. It also uses XStream to configure itself so you can add an attribute alias with <attributeAlias> elements in the XML mapping configuration. You can also add type converters for converting Objects as a whole to and from XML if you need to override the default behaviour.

All of this is under review and hasn't really come under any close scrutiny yet., hence the implementation might be a bit volatile. We are quite keen to use Spring OXM if at all possible - but this might be a longer term plan, because there is no support for streaming there at the moment.

Jim Collins
Apr 25th, 2008, 05:02 AM
Hi Dave. Do you have any examples of using attributeAlias? I am trying to do the same thing.

Thanks

Jim

Dave Syer
Apr 25th, 2008, 09:08 AM
We don't use XStream directly any more, we delegate to Spring OXM, which wraps it in a marshaller/unmarshaller. The Xstream docos for that are here: http://tinyurl.com/65kq8f (there's a link to the XStream website there as well). The sample job that uses XML actually happens to be an XStream example as well (see xmlStaxJob.xml).

Jim Collins
Apr 25th, 2008, 11:26 AM
I am using the marshaller and unmarshaller. I tried to set the useAttributeFor property on XStreamMarshaller using the same mechanism as you do for setting the aliases. This did not work for me and I got an error setting the wrong property type.

If I had any control over the XML I would change the attribue to a element but I can't.