________________________________
From: tushar mishra <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Sunday, 25 September 2011 4:36 PM
Subject: problem with SetPropertyRule
Hi,
I am facing problem running a simple example using digester.
Reference - http://commons.apache.org/digester/guide/core.html
File Name - foo.xml
<?xml version='1.0'?>
<foo fooName="The Parent">
<bar id="123" title="The First Child" />
<bar id="456" title="The Second Child" />
</foo>
File Name - Foo.javapackage mypackage;
import java.util.Iterator;
public class Foo
{String name;public String getName(){
return name;
}
public void setName(String name)
{
this.name = name;
}
}
File Name - Client.java
package mypackage;
import org.apache.commons.digester3.Digester;
public class Client
{
public static void main(String[] args)
{
String filename = "C:\\development\\projects\\test\\resources\\foo.xml";
Foo foo = null;
Digester d = new Digester();
d.setValidating(false);
addRules(d);
// Process the input file.
try
{
java.io.File srcfile = new java.io.File(filename);
foo = (Foo) d.parse(srcfile);
}
catch (java.io.IOException ioe)
{
System.out.println("Error reading input file:" + ioe.getMessage());
System.exit(-1);
}
catch (org.xml.sax.SAXException se)
{
System.out.println("Error parsing input file:" + se.getMessage());
System.exit(-1);
}
System.out.println(foo.getName());
}
private static void addRules(Digester digester)
{
digester.addObjectCreate("foo", "mypackage.Foo");
digester.addSetProperty("foo", "fooName", "name");}
}
Output
Error parsing input file:Error at line 2 char 27: Bean has no property named
The Parent
Sep 25, 2011 4:12:48 PM org.apache.commons.digester3.Digester startElement
SEVERE: Begin event threw exception
java.lang.NoSuchMethodException:Bean has no property named The Parent
at org.apache.commons.digester3.SetPropertyRule.begin(SetPropertyRule.java:133)
at org.apache.commons.digester3.Digester.startElement(Digester.java:1319)
Question & Analysis
Class - SetPropertyRule
if ( attributeName.equals( this.name ) )
{
actualName = value;
}
1. What is this check for ? The documents gets parsed. It also finds the
attribute names correctly. But here, due to this condition, the attribute name
- fooName changes to "The Parent".
What am I missing ?
Thanks for your time.
- Tushar