Hi all, I´m having a problem trying to parse mixed collections. This is what I obtaint if I parse my bean to a XML file:

<?xml version='1.0' encoding="ISO-8859-1"?>
 <container>
   <element>
     <atr1>atr1</atr1>
     <atr2>atr2</atr2>
   </element>
   <element>
     <atr21>atr21</atr21>
     <otherelement>otherelemt1</otherelement>
     <otherelement>otherelemt2</otherelement>
     <otherelement>otherelemt3</otherelement>
     <otherelement>otherelemt4</otherelement>
   </element>
 </container>

This is correct and it´s the result I´m expecting. The problem comes when I try to parse this XML into a bean. Betwixt seems not to recognice the type of the elements under the Vector and it create instances of Object instead the right ones. If I parse the above XML into a bean and then into a XML again I obtaint:

<?xml version='1.0' encoding="ISO-8859-1"?>
 <container>
   <element/>
   <element/>
 </container>

Every element is parsed as Object. The source code I´m using (including the dot betwixt files) is attached to this mail.

Note that if I turn on MapIDs property under the binding configuration the result is correct but I get some id="number" attributes that must not be there for my needs.

Thanks in advance.
package testbetwixt;

public class Parser {

        public static void main(String[] args) {
                Container c = new Container();
                
                ElementImpl1 e1 = new ElementImpl1();
                e1.setAtr1("atr1");
                e1.setAtr2("atr2");
                
                ElementImpl2 e2 = new ElementImpl2();
                e2.setAtr1("atr21");
                e2.addOtherElement("otherelemt1");
                e2.addOtherElement("otherelemt2");
                e2.addOtherElement("otherelemt3");
                e2.addOtherElement("otherelemt4");
                
                c.addElement(e1);
                c.addElement(e2);

                try {
                        System.out.println(c.toXML());
                        System.out.println("<======================>");
                        
System.out.println(Container.parseXML(c.toXML()).toXML());
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }

}
<?xml version='1.0' encoding='UTF-8' ?>
<info>
	<element name='container'>
		<element property='elements' adder='addElement' />
	</element>
</info>
package testbetwixt;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Vector;

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

public class Container {
        private Vector elements;

        public Container() {
                this.elements = new Vector();
        }

        public void addElement(Object element) {
                this.elements.add(element);
        }

        public Collection getElements() {
                return this.elements;
        }

        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.getBindingConfiguration().setMapIDs(false);
                beanWriter.enablePrettyPrint();

                beanWriter.write("container", this);

                String ret = outputWriter.toString();

                outputWriter.close();

                return ret;
        }

        public static Container parseXML(String xml) throws IOException, 
SAXException, IntrospectionException {
                StringReader xmlReader = new StringReader(xml);

                BeanReader beanReader = new BeanReader();
                beanReader.getBindingConfiguration().setMapIDs(false);

                beanReader.registerBeanClass("container", Container.class);

                return (Container) beanReader.parse(xmlReader);
        }
}
<?xml version='1.0' encoding='UTF-8' ?>
<info>
	<element name='impl1'>
		<element name='atr1' property='atr1'/>
		<element name='atr2' property='atr2'/>
	</element>
	<addDefaults/>
</info>	
package testbetwixt;

public class ElementImpl1 {
        private String atr1;
        private String atr2;
        
        public ElementImpl1() {
                this.atr1 = "";
                this.atr2 = "";
        }
        
        public void setAtr1(String atr1) {
                this.atr1 = atr1;
        }

        public String getAtr1() {
                return atr1;
        }
        
        public void setAtr2(String atr2) {
                this.atr2 = atr2;
        }

        public String getAtr2() {
                return atr2;
        }

}
<?xml version='1.0' encoding='UTF-8' ?>
<info>
	<element name='impl2'>
		<element name='atr21' property='atr1'/>
		<element name='otherelement' property='otherElementIterator' adder='addOtherElement' />
	</element>
	<addDefaults/>
</info>	
package testbetwixt;

import java.util.Iterator;
import java.util.Vector;

public class ElementImpl2 {
        private String atr1;
        private Vector moreElements;
        
        public ElementImpl2() {
                this.atr1 = "";
                this.moreElements = new Vector();
        }
        
        public void setAtr1(String atr1) {
                this.atr1 = atr1;
        }

        public String getAtr1() {
                return atr1;
        }
        
        public void addOtherElement(String elemento) {
                this.moreElements.add(elemento);
        }
        
        public Iterator getOtherElementIterator() {
                return this.moreElements.iterator();
        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to