我对 Apache Digester 有疑问,因为它在当前对象上调用set 方法,而不是在与 parent tag 对应的对象中调用。结果是 NoSuchMethodException。
Note: I'm reusing the Digester object due to performance problems, but I'm calling clear() method before each parse.
这是我的代码:(它在 Header 对象而不是Message 对象上调用 setHeader方法)。
XML:
<message>
<header id_message="2014871" date="07/08/2014 21:56:41" />
<body>
...
</body>
</message>
以及创建消化器的代码:
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");
它工作正常,但是当它处理一些 xml 时,它会尝试在 Header 对象中调用 setHeader 方法:
[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)
...
我查看了 javadoc ( http://commons.apache.org/proper/commons-digester/commons-digester-2.0/docs/api/ ),代码似乎没问题(第二个参数是方法名称调用父元素):
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)