Try this :
TestBean bean = new TestBean();
Digester digester = new Digester();
digester.setValidating(false);
digester.push(bean);
digester.addObjectCreate("toplevel/element/");
digester.addCallMethod("toplevel/element/field1", "setField1", 0);
digester.addCallMethod("toplevel/element/field2", "setField2", 0);
digester.parse (blah);
and you have a nice bean (mind the ending / on addObjectCreate.
Probably betwixt will serve a better needs for your needs and (can be)
simpeler (check it out from cvs, and look at the testcases for
examples..)
Mvgr,
Martin
On Tue, 2002-07-02 at 22:46, Brad Jones wrote:
> I am new to XML parsing (from an xml file to a Java object) and I'm trying
> out Digester.
> I'm having trouble figuring out how to use the digester to access the parsed
> XML once i've run the digester.parse() method. FYI...The following examples
> are just testing code for me to learn digester so things like Exception
> handling are not fully written.
>
> Here's a very simple xml file that I"m parsing:
>
> <toplevel>
> <element>
> <field1>Value 1</field1>
> <field2>Value 2</field2>
> </element>
> </toplevel>
>
> TestPropertyBean tpbean = new TestPropertyBean();
>
> java.io.FileInputStream fileInputStream = null;
> Digester digester = new Digester();
>
> //Adding data structure to digester to be parsed
> digester.push(tpbean);
>
> digester.setValidating(false);
>
> //Rules for parsing the xml file.
> digester.addObjectCreate("toplevel/element",
> "com.ibm.us.force.bean.property.TestPropertyBean");
> digester.addCallMethod("toplevel/element/field1","setField1",1);
> digester.addCallParam("toplevel/element/field1", 0);
> digester.addCallMethod("toplevel/element/field2","setField2",1);
> digester.addCallParam("toplevel/element/field2", 0);
>
> try {
> fileInputStream = new java.io.FileInputStream(name);
> } catch (java.io.FileNotFoundException fnfe) {
> //asdf
> }
>
> try {
> Object data = digester.parse(fileInputStream);
> TestPropertyBean tpbean2 = (TestPropertyBean)data;
> fileInputStream.close();
> } catch (org.xml.sax.SAXException se) {
> //asdf
> System.out.println(se.getMessage());
> } catch (java.io.IOException ie) {
> //asdf
> }
>
> When the digester.parse() method is called, I can see the TestPropertyBean
> setters being called and the values being set to the bean instance. My
> problem is that I don't know how to get access to that data once it's been
> parsed. I put break points in my setters on my bean and they are being
> called so I know that the data is being parsed from the XML to the bean but
> then I can't seem to get the "populated" object to return because when I
> cast to a TestPropertyBean the values are equal to null not "value 1",
> "value 2".
> I think it's a problem with the rules that I've written but I can't seem to
> figure out the correct combination.
>
> any suggestions would be greatly appreciated.
>
> Brad
>
>
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> --
> To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>