Reputation: 461
I have a problem with Apache Digester because it's calling set method on the current object instead of in the object that corresponds to parent tag. The result is a NoSuchMethodException.
Note: I'm reusing the Digester object due to performance problems, but I'm calling clear() method before each parse.
Here is my code: (It is calling setHeader method on Header object instead of Message object).
The XML:
<message>
<header id_message="2014871" date="07/08/2014 21:56:41" />
<body>
...
</body>
</message>
And the code to create the digester:
digesterSOBB = new Digester();
digesterSOBB.setValidating(true);
digesterSOBB.setSchema(XMLParser.class.getResource(dtdUrlSOBB).getFile());
digesterSOBB.register("message", XMLParser.class.getResource(dtdUrlSOBB));
digesterSOBB.setErrorHandler(new XMLDtdValidationErrorHandler());
//Message
digesterSOBB.addObjectCreate("message", "com.company.utils.xmlparser.beans.sosc.Message");
digesterSOBB.addSetProperties("message");
//Header-I
digesterSOBB.addObjectCreate("message/header", "com.company.utils.xmlparser.beans.sosc.Header");
digesterSOBB.addSetProperties("message/header");
digesterSOBB.addSetNext("message/header", "setHeader", "com.company.utils.xmlparser.beans.sosc.Header");
It is working properly but, when it process some xmls, it try to call setHeader method in Header object:
[8/7/14 5:22:00:322 CEST] 00000061 Digester E org.apache.commons.logging.impl.Jdk14Logger error End event threw exception
java.lang.NoSuchMethodException: No such accessible method: setHeader() on object: com.company.utils.xmlparser.Header
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:278)
at org.apache.commons.digester.SetNextRule.end(SetNextRule.java:217)
at org.apache.commons.digester.Rule.end(Rule.java:253)
at org.apache.commons.digester.Digester.endElement(Digester.java:1332)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
...
I've reviewed the javadoc (http://commons.apache.org/proper/commons-digester/commons-digester-2.0/docs/api/), and the code seems to be ok (the second parameter is the method name to call on the parent element):
public void addSetNext(String pattern,
String methodName,
String paramType)
Add a "set next" rule for the specified parameters.
Parameters:
pattern - Element matching pattern
methodName - Method name to call on the parent element
paramType - Java class name of the expected parameter type (if you wish to use a primitive type, specify the corresonding Java wrapper class instead, such as java.lang.Boolean for a boolean parameter)
Upvotes: 1
Views: 2291
Reputation: 21
I have not used this library, but looking over the manual, it is stack based - is there an ordering issue when referring to the parent (Message) which presumably is under the child (Header), the newer object being on top of the stack?
The FAQ has a little information about this: http://wiki.apache.org/commons/Digester/FAQ
See -> How do I get CallMethodRule to fire before SetNextRule? Where the suggested example sequence is:
digester.addObjectCreate("parent", Parent.class);
digester.addObjectCreate("parent/child", Child.class);
digester.addSetNext("parent/child", "addChild");
digester.addCallMethod("parent/child", "setName", 1);
digester.addCallParam("parent/child/name", 0);
Hope this helps.
Upvotes: 1