db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Greg Monroe" <Greg.Mon...@DukeCE.com>
Subject RE: Any Interest in Betwixt Map Templates?
Date Fri, 30 Jun 2006 19:29:15 GMT
> Jim Caserta said:
> 
> So betwixt can map a Java Bean to XML.. Just the opposite of 
> JAXB, XML to Java?

Yeah, that's a good way to think of it.  JAXB is the Torque 
of XML objects. Takes XML Schema's and creates the objects 
that can read and write matching XML.

Betwixt takes Bean like objects, used Bean Introspection to 
find the properties and map the info in the object to XML 
using a simple set of rules.  It also does the reverse in 
reading in XML and creating populated objects.

The defaults rules are pretty good and if you're code is 
Bean safe and you don't care about verbage/DTD's, you can 
use it without creating any betwixt "mapping" files.  But 
you can get "fancy" if you create mapping files that define 
what properties to pay attention to/ignore.  This lets you 
use Betwixt to import / export data as/from XML with some 
simple code like:

----------------------------------------------------------
Users test;
Criteria c = new Criteria();
c.add(UsersPeer.USERNAME,(Object) "admin",SqlEnum.EQUAL);
List results = UsersPeer.doSelect(c);
if ( results == null ) {
    System.out.println("results was null!");
    return;
}
test = (Users) results.get(0);

// Start by preparing the writer
FileWriter outputWriter = new FileWriter("test.xml"); 
        
// Betwixt just writes out the bean as a fragment
// To get well-formed xml, we need to add the prolog
outputWriter.write("<?xml version='1.0' ?>\n");
outputWriter.write(
   "<!DOCTYPE dataset SYSTEM "bookstore-betwixt.dtd">");
outputWriter("<dataset> name=\"bookstore\">");
        
// Create a BeanWriter which writes to our output stream
BeanWriter beanWriter = new BeanWriter(outputWriter);
        
// Configure betwixt
beanWrite.registerMultiMapping(new InputSource(
                  new FileReader("bookstore.betwixt")));
beanWriter.getXMLIntrospector().getConfiguration().
                  setAttributesForPrimitives(false);
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.enablePrettyPrint();
        
// Write Torque 
beanWriter.write(test);

outputWriter("</dataset>");
outputWriter.close();
----------------------------------------------

Note that with the Generated MultiMapping file, the
beanWriter call will work with any type of Torque 
record object in your schema.

To read in XML, just use similar code with BeanReader.



Duke CE Privacy Statement
Please be advised that this e-mail and any files transmitted with it are confidential communication
or may otherwise be privileged or confidential and are intended solely for the individual
or entity to whom they are addressed.  If you are not the intended recipient you may not rely
on the contents of this email or any attachments, and we ask that you  please not read, copy
or retransmit this communication, but reply to the sender and destroy the email, its contents,
and all copies thereof immediately.  Any unauthorized dissemination, distribution or copying
of this communication is strictly prohibited.



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


Mime
View raw message