commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lena...@email.arizona.edu
Subject Re: Betwixt: BeanReader setup question
Date Wed, 09 Apr 2008 18:32:59 GMT
Nathalie,

So I have an example working....

Given the input XML file (me.xml):
----------------------------------
<?xml version='1.0' encoding='UTF-8' ?>
<me id="he3432h11-032">
    <a name="rock hard" />
    <b name="monkey" />
    <c name="scam tactics" />
</me>

MeElement.java
----------------------------------
public class MeElement {
	private String id;
	private String nameA;
	private String nameC;
  /* I have getters/setters defined for omitting them here */
}

MeElement.betwixt
----------------------------------
<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes="element">
   <element name="me">
     <attribute name="id" property="id" />
     <element name="a">
       <attribute name="name" property="nameA" />
     </element>
     <element name="c">
       <attribute name="name" property="nameC" />
     </element>
     <addDefaults/>
   </element>
</info>

Remember that the .betwixt file needs to be in the same directory that your
.java file is.  Betwixt will pick up both theses files since they're on the
classpath together.

I'll assume that you have JUnit setup (because we all know that testing is
important...even former .NET people like me).  So I have a subclass of 
TestCase
with the following method defined:

public void testMeDocumentLoad() {
   try { // this assumes that 'me.xml' is sitting on the classpath
     InputStream xtnodes = getClass().getResourceAsStream("./me.xml");
     BeanReader beanReader = new BeanReader();
     beanReader.registerBeanClass(MeElement.class);
     MeElement root = (MeElement)beanReader.parse(xtnodes);
     assertNotNull(root);
     // add further testing to ensure that the bean/pojo has been loaded as
expected
     System.out.println(root);
   } catch (Exception e) {
     e.printStackTrace();
     System.out.println("exception occured... lame");
   }
}

Betwixt does have log4j capabilities (apparently).  I tried to 
configure it for
my unit test but I'm not getting any INFO output.  I'm not really too familiar
with log4j - so I can't really offer any help.  I just was able to 
confirm that
you should be able to get DEBUG/INFO/WARN information about Betwixt via log4j.

I hope that helps,

Andy

/****************************************
  * @author:  Andrew Lenards
  * @email:   lenards@email.arizona.edu
  * @title:   Systems Programmer, Principal
  * @project: Tree of Life Web Project
  * @website: http://tolweb.org/
  * @dept:    Dept. of Entomology
  * @school:  University of Arizona
  * @geo-loc: Tucson, AZ 85721
  ***************************************/


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


Mime
View raw message