commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Diego Amicabile" <1822-...@onlinehome.de>
Subject [Betwixt] Example to learn
Date Sat, 01 May 2004 21:41:24 GMT
An example I created myself to better understand some things of betwixt. I am new both to betwixt
and digester.
I am using the common lang as well


//Order.java
import org.apache.commons.lang.builder.ToStringBuilder;

public class Order {
    private int id;
 

 public int getId() {
  return id;
 }


 public void setId(int id) {
  this.id = id;
 }
 
 public Order(int newId) {
   id = newId;
 }
 
 public Order() {
 }

 public String toString() {
  return ToStringBuilder.reflectionToString(this);
 }
 
 
}



// CustomerBean.java
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.*;

public class CustomerBean {
 
 
    private String name;
    private Collection orders = new ArrayList();
    private Collection emailAddres = new ArrayList();
 public String getName() {
  return name;
 }

 public Collection getOrders() {
  return orders;
 }


 public void addEmailAddre(String newAddress) {
  emailAddres.add(newAddress);    
 }

 public Collection getEmailAddres() {
  return emailAddres;
 }

 public void setName(String name) {
  this.name = name;
 }


 public void addOrder(Order newOrder) {
  orders.add(newOrder);    
 }


 public String toString() {
  return ToStringBuilder.reflectionToString(this);
 }
}

I want to create and read XML files in the format

<?xml version='1.0' ?>
<customerBean>
  <emailAddre>A</emailAddre>
  <emailAddre>B</emailAddre>
  <emailAddre>C</emailAddre>
  <name>Diego</name>
  <order id="1"/>
  <order id="2"/>
</customerBean>

How do I do that ? 
Answer 
This is my CustomerBean.betwixt


<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='attribute'>
<element name='customerBean'>
<element name='emailAddre' property='emailAddres'>
</element>
<element name='name' property='name'/>
<element name='order' property='orders'>
<attribute name='id' property='id'>
</attribute>
</element>
</element>
</info>

This is to write the file to a xml file called CustomerBean.xml


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;

public class WriteCustomerBeanApp {

    public static final void main(String [] args) throws Exception {
        
       StringWriter outputWriter = new StringWriter(); 
       outputWriter.write("<?xml version='1.0' ?>");

        BeanWriter beanWriter = new BeanWriter(outputWriter);
   
        beanWriter.getXMLIntrospector().setAttributesForPrimitives(false);
        beanWriter.setWriteIDs(false);
        beanWriter.enablePrettyPrint();
        beanWriter.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper());
   
        beanWriter.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper());

        CustomerBean customerBean = new CustomerBean();
        customerBean.setName("Diego");

        customerBean.addEmailAddre("A");
        customerBean.addEmailAddre("B");
        customerBean.addEmailAddre("C");
 
  customerBean.addOrder(new Order(1));
  customerBean.addOrder(new Order(2));

        beanWriter.write("customerBean", customerBean);
  
        System.out.println(outputWriter.toString());
        FileOutputStream fos = new FileOutputStream( new File( "CustomerBean.xml" ));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(fos)); 
        out.println(outputWriter.toString());
        out.println();
        out.flush();
        out.close();
    }        
}

This to read it into a live object 


import java.io.FileReader;

import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;

public class ReadCustomerBeanApp {
    
    public static final void main(String args[]) throws Exception{

     FileReader xmlReader = new FileReader("CustomerBean.xml");

        BeanReader beanReader  = new BeanReader();
        
        beanReader.getXMLIntrospector().setAttributesForPrimitives(false);
        beanReader.setMatchIDs(false);
        beanReader.getXMLIntrospector().setAttributeNameMapper(new HyphenatedNameMapper());
        beanReader.getXMLIntrospector().setElementNameMapper(new DecapitalizeNameMapper());
    
       
        beanReader.registerBeanClass("customerBean", CustomerBean.class);
        beanReader.registerBeanClass("order", Order.class);
        CustomerBean customerBean = (CustomerBean) beanReader.parse(xmlReader);
        System.out.println(customerBean);
    }
    
}


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