Hello, here is some code for you. This code will create a class with a
LinkedList. The elements of this list are Strings. The whole structure
is parsed to XML. I have tried to parse the XML into a bean and it works
too, and re-parse to XML again. The code is attachement.
Hope this help you.
denis queffeulou escribió:
Sorry I've just read a mail in the archive for the subject [XXX]
I've found the DTD in another archive mail (but still no adder
attribute, which seems to have been replaced with updater ?)
So if I use :
<element name="recipients">
<element name="recipient" property="recipients"
updater="addRecipient"/>
</element>
The addRecipient method is called two times with a *null* parameter.
package testbetwixt;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.ListIterator;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;
public class ListContainer {
private LinkedList theList;
public ListContainer() { this.theList = new LinkedList(); }
public ListIterator getElementIterator() { return
this.theList.listIterator(); }
public void addElement(String element) { this.theList.add(element); }
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("somecontainerelement", this);
String ret = outputWriter.toString();
outputWriter.close();
return ret;
}
public static ListContainer parseXML(String xml) throws IOException,
SAXException, IntrospectionException {
StringReader xmlReader = new StringReader(xml);
BeanReader beanReader = new BeanReader();
beanReader.getBindingConfiguration().setMapIDs(false);
beanReader.registerBeanClass("somecontainerelement",
ListContainer.class);
return (ListContainer)beanReader.parse(xmlReader);
}
}
package testbetwixt;
public class Parser {
/**
* @param args
*/
public static void main(String[] args) {
ListContainer itemToParse = new ListContainer();
itemToParse.addElement("foo");
itemToParse.addElement("bar");
itemToParse.addElement("09089787767");
itemToParse.addElement("767576565");
try {
System.out.println("Item to XML");
System.out.println(itemToParse.toXML());
System.out.println("XML to Item");
ListContainer anotherListContainer =
ListContainer.parseXML(itemToParse.toXML());
System.out.println("The parsed item to XML");
System.out.println(anotherListContainer.toXML());
} catch(Exception e) {
e.printStackTrace();
}
}
}
<?xml version='1.0' encoding='UTF-8' ?>
<info>
<element name='somecontainerelement'>
<element name='element' property='elementIterator' adder='addElement' class='java.lang.String' />
<addDefaults/>
</element>
</info>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]