commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcos Truchado Martín <marcos.truchado.mar...@hp.com>
Subject problem using betwixt
Date Thu, 16 Feb 2006 16:25:51 GMT
Given this code:

------------------------------------------------------------------------

/package testbetwixt;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.ListIterator;

import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;

public class Class1 {
   
    private String field1;
    private String field2;
    private LinkedList things;
   
    public Class1() {
        this.field1 = "";
        this.field2 = "";
        this.things = new LinkedList();
    }
   
    public String getField1() { return this.field1;}
    public void setField1(String field1) { this.field1 = field1;}
   
    public String getField2() { return field2; }
    public void setField2(String field2) { this.field2 = field2; }
   
    public void addThing(Thing thing) { things.add(thing); }
    public ListIterator getThingIterator() { return things.listIterator(); }

    public String toXML() throws IOException, IntrospectionException, 
SAXException {
        StringWriter outputWriter = new StringWriter();
       
        outputWriter.write("<?xml version='1.0' 
encoding=\"ISO-8859-1\"?>\n");
        BeanWriter beanWriter = new BeanWriter(outputWriter);

        
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
        
beanWriter.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
        beanWriter.getBindingConfiguration().setMapIDs(false);
        beanWriter.enablePrettyPrint();
       
        beanWriter.write("first-element", this);
       
        String ret = outputWriter.toString();
        outputWriter.close();
       
        return ret;
    }
}/

------------------------------------------------------------------------

/package testbetwixt;

public class Thing {
    private String field3;
   
    public Thing() {
        this.field3 = "";
    }
   
    public String getfield3() { return field3; }
    public void setfield3(String field3) { this.field3 = field3; }
}/

------------------------------------------------------------------------

And the following class that makes some beans and pass it to XML:

/package testbetwixt;

public class Parser {
    public static final void main(String [] args) {
       
        Class1 c = new Class1();
        c.setField1("1");
        c.setField2("2");
       
        Thing t1 = new Thing();
        t1.setfield3("3");
       
        Thing t2 = new Thing();
        t2.setfield3("4");
       
        c.addThing(t1);
        c.addThing(t2);

       
        try {
            System.out.println(c.toXML());
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }

}/

------------------------------------------------------------------------

I´m using these .betwixt files:

For Class1:

/<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='attribute'>
    <element name='first-element'>
        <attribute name="foo" property="field1"/>
        <attribute name="bar" property="field2"/>
        <addDefaults/>
    </element>
</info>/

and this one is for Thing:

/<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes='attribute'>
    *<element name='second-element'>*
        <attribute name="inside-thing" property="field3"/>
    </element>
</info>/

The problem I´m having is that I get this XML:

/<?xml version='1.0' encoding="ISO-8859-1"?>
  <first-element foo="1" bar="2">
    <thing inside-thing="3"/>
    <thing inside-thing="4"/>
  </first-element>/

but my desired ouput is:

/<?xml version='1.0' encoding="ISO-8859-1"?>
  <first-element foo="1" bar="2">
    <*second-element* inside-thing="3"/>
    <*second-element *inside-thing="4"/>
  </first-element>/

where is my fault?

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