commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sameer Nanda <sam5...@gmail.com>
Subject Fwd: Managing XML Output from Betwixt
Date Mon, 03 Oct 2005 16:55:59 GMT
Hi Tom,

That will help I guess ( I dint try ) but the way you explained looks like
its gonna do some magic.
Ok but heres the thing, right now I got it working the exact same way I want
I just included this line in my Main class:

beanWriter.getXMLIntrospector().setElementNameMapper(new
DecapitalizeNameMapper());

and it did the trick, so now my XML is generated as I want it to be:

<user>
<authorizations>
<authorization>
<functionName>read</functionName>
<objectType>employee</objectType>
</authorization>
<authorization>
<functionName>read</functionName>
<objectType>customer</objectType>
</authorization>
<authorization>
<functionName>write</functionName>
<objectType>employee</objectType>
</authorization>
<authorization>
<functionName>write</functionName>
<objectType>customer</objectType>
</authorization>
</authorizations>
<firstName>Demo</firstName>
<id>569</id>
<lastName>Demo</lastName>
</user>
=======================================

But may be for my future I will keep this trick in mind and try using it. I
havent actually started to use customizing the mappings of a beans to XML,
but I think in the long run its gonna help, so I am kind of reading it right
now and will definetly try the method you said.

Thanks again Russell, Tom. You guys are awesome. May be I will be back if I
get stuck up with the advanced mapping of Beans to XMl.

-Sameer

---------- Forwarded message ----------
From: Thomas Dudziak <tomdzk@gmail.com>
Date: Oct 3, 2005 12:08 PM
Subject: Re: Managing XML Output from Betwixt
To: Jakarta Commons Users List <commons-user@jakarta.apache.org>, Sameer
Nanda <sam504u@gmail.com>

On 10/3/05, Sameer Nanda <sam504u@gmail.com> wrote:
> Well thanks again Russell,
>
> Whatever you said is true, I can either rename my Class to a lowercase
> authorization (but then I am breaking the naming convention for writing
Java
> classes which I dont wanna do) , and I could also run the replaceAll() on
> the XML string generated to replace all <Authorization> to <authorization>
> (but that would be a hack).
>
> My point here is, it shouldnt have to be like that ....... theres gotta be
> something in Betwixt using which I can do this. I mean this is just one
> example I am talking here, I have to generate much more complex XML
> documents which would involve a lot of Java Bean Objects, which in turn
> would contain a bunch of other Java Beans. Do you know what I mean?
>
> Again, Russell its not that I appreciate your ideas and the time you have
> spent with me discussing my problem, but all I want is to know if theres
> something in Betwixt using which I can get this problem solved !!!!
>
> And I was still woundering and looking for answers to know if its the
> java.util.List with which Betwixt is behaving funny or is there something
> else I am missing or not doing right here ???

I think you have to do two things:

* tweak the mapping (see below), and
* add an additional method to User which is important for the mapping:

public void addAuthorization(Authorization authorization)
{
authorizations.add(authorizations);
}


Now (assuming you're using betwixt 0.7) you have to tweak the mapping
using an additional mapping.xml file:

<?xml version="1.0"?>
<betwixt-config>
<class name='mms.Objects.User'>
<element name='user'>
<element name='authorizations'>
<element name='authorization'
property='authorizations'
updater='addAuthorization'/>
</element>
<addDefaults/>
</element>
</class>
<class name='mms.Objects.Authorization'>
<element name='authorization'>
<addDefaults/>
</element>
</class>
</betwixt-config>

Now prior to reading/writing via betwixt you set this mapping:


public Object readXML(Reader mappingReader, Reader xmlReader)
throws IOException, SAXException, IntrospectionException
{
BeanReader beanReader = new BeanReader();

beanReader.registerMultiMapping(new InputSource(mappingReader));
return beanReader.parse(xmlReader);
}

public void writeXML(Reader mappingReader, Writer xmlWriter,
Object obj) throws IOException, SAXException, IntrospectionException
{
xmlWriter.write("<?xml version='1.0' ?>\n");

BeanWriter beanWriter = new BeanWriter(xmlWriter);

beanWriter.getXMLIntrospector().register(new
InputSource(mappingReader));
beanWriter.enablePrettyPrint();
beanWriter.write(obj);
}


where mappingReader is a Reader that retrieves the above mapping (eg.
a FileReader or StringReader).

Hope that helps,
Tom

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message