Dear Wiki user, You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.
The following page has been changed by SimonKitching: http://wiki.apache.org/jakarta-commons/Digester/FAQ The comment on the change is: Add examples for populating List and Map objects ------------------------------------------------------------------------------ not experience the "reverse call" order that applies to the "end" method of Rules. + === How do I add literal elements to a List object? === + + Assume the following input: + {{{ + <list> + <entry>value1</entry> + <entry>value2</entry> + <entry>value3</entry> + <entry>value4</entry> + </list> + }}} + + The following rules will generate and populate an !ArrayList with the literal strings: + {{{ + digester.addObjectCreate("list", ArrayList.class); + digester.addCallMethod("list/entry", "add", 1); + digester.addCallParam("list/entry", 0); + }}} + + === How do I add object elements to a List object? === + + Assume the following input: + {{{ + <list> + <entry>value1</entry> + <entry>value2</entry> + <entry>value3</entry> + <entry>value4</entry> + </list> + }}} + + The following rules will generate and populate an !ArrayList with an object + representing each entry. In this case, a !StringBuffer is generated to represent + each entry, but of course the entry xml could be more complex, and mapped into + some appropriate more complex object: + {{{ + digester.addObjectCreate("list", ArrayList.class); + + // rules to create/initialise an object for each entry + digester.addObjectCreate("list/entry", StringBuffer.class); + digester.addCallMethod("list/entry", "append", 1); + digester.addCallParam("list/entry", 0); + + // rule to add the Entry object (top object on the stack) + // to the list object (top-1 object on the stack) + digester.addSetNext("list/entry", "add"); + }}} + + === How do I add literal elements to a Map object? === + + Assume the following input: + {{{ + <map> + <entry key='key1'>value1</entry> + <entry key='key2'>value2</entry> + <entry key='key3'>value3</entry> + <entry key='key4'>value4</entry> + </map> + }}} + + The following rules will generate and populate a !HashMap: + {{{ + digester.addObjectCreate("map", HashMap.class); + + // call the put method on the top object on the digester stack + // passing the key attribute as the 0th parameter + // and the element body text as the 1th parameter.. + digester.addCallMethod("map/entry", "put", 2); + digester.addCallParam("map/entry", 0, "key"); + digester.addCallParam("map/entry", 1); + }}} + + === How do I add object elements to a Map object? === + + Assume the following input: + {{{ + <map> + <entry key='key1'>value1</entry> + <entry key='key2'>value2</entry> + <entry key='key3'>value3</entry> + <entry key='key4'>value4</entry> + </map> + }}} + + The following rules will generate and populate a !HashMap with an object + representing each entry. In this case, a !StringBuffer is generated to represent + each entry, but of course the entry xml could be more complex, and mapped into + some appropriate more complex object: + {{{ + digester.addObjectCreate("map", HashMap.class); + + // create an object to represent the entry and initialise it + digester.addObjectCreate("map/entry", StringBuffer.class); + digester.addCallMethod("map/entry", "append", 1); + digester.addCallParam("map/entry", 0); + + // call the put method on the second-to-top object on the digester stack + // passing the key attribute as the 0th parameter + // and the top element on the stack as the 1th parameter.. + // + // Note that we can't use SetNextRule as that only ever passes one parameter. + // Note also that this variant of CallMethodRule doesn't have a convenience + // factory method on the Digester class, so we need to create it directly. + Rule r = new CallMethodRule(1, "put", 2); + digester.addRule("map/entry", r); + digester.addCallParam("map/entry", 0, "key"); + digester.addCallParam("map/entry", 1, true); + }}} + --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
