commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Dudziak <tom...@gmail.com>
Subject Re: Managing XML Output from Betwixt
Date Mon, 03 Oct 2005 16:08:23 GMT
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

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message