commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jimmy Zhang" <crack...@comcast.net>
Subject Re: [digester] Need help with simple digester usage
Date Thu, 13 Jan 2011 22:24:36 GMT
You may want to look at extended vtd-xml, which is significantly better than
digester in memory and performance, and ease of use...

-----Original Message----- 
From: KARR, DAVID (ATTSI)
Sent: Thursday, January 13, 2011 9:39 AM
To: Commons Users List
Subject: RE: [digester] Need help with simple digester usage

> -----Original Message-----
> From: Rahul Akolkar [mailto:rahul.akolkar@gmail.com]
> Sent: Monday, December 20, 2010 10:18 PM
> To: Commons Users List
> Subject: Re: [digester] Need help with simple digester usage
>
> On Mon, Dec 20, 2010 at 11:08 PM, KARR, DAVID (ATTSI) <dk068x@att.com>
> wrote:
> >> -----Original Message-----
> >> From: KARR, DAVID (ATTSI)
> >> Sent: Monday, December 20, 2010 7:54 PM
> >> To: user@commons.apache.org
> >> Subject: [digester] Need help with simple digester usage
> >>
> >> I'm trying to use Digester for a simple application.  I want to
> define
> >> the structure of a binary tree in XML, and then construct that node
> >> tree
> >> using Digester.
> >>
> >> The trivial "Node" class is just this:
> >> --------------------
> >>     public static class Node {
> >>         private Node left;
> >>         private Node right;
> >>         private String value;
> >>
> >>         public Node getLeft() { return left; }
> >>         public Node getRight() { return right; }
> >>         public String getValue() { return value; }
> >>
> >>         public void setLeft(Node left) { this.left = left; }
> >>         public void setRight(Node right) { this.right = right; }
> >>         public void setValue(String value) { this.value = value; }
> >>     }
> >> ------------
> >>
> >> The XML is composed of nested "node" elements.  The first "node"
> child
> >> gets set as the "left" property of the root, and the second gets set
> > as
> >> the "right" property.
> >>
> >> For instance:
> >>
> >>   <node value="abc">
> >>     <node value="def"/>
> >>   </node>
> >>
> >> Should result in a root Node with a "left" property referring to the
> >> second node.
> >>
> >> So, going through the Digester documentation, which I haven't looked
> > at
> >> in many years, I figured it would be something like this:
> >>
> >>         digester.addRule("*/node", new
> >> ObjectCreateRule(AugmentedNode.class));
> >>         digester.addRule("*/node", new SetPropertiesRule());
> >>         digester.addRule("*/node", new SetNextRule("addChild",
> >> AugmentedNode.class.getName()));
> >>
> >> Where "AugmentedNode" extends "Node", adding:
> >>
> >>        public void addChild(Node node) {
> >>             if (getLeft() == null)
> >>                 setLeft(node);
> >>             else
> >>                 setRight(node);
> >>         }
> >>
> >> For those well steeped in Digester lore, I imagine you can
> immediately
> >> tell this won't work.  You're right, of course.  This dies with an
> NPE
> >> in "MethodUtils.invokeMethod()" (object is null).  I have no clue
> >> what's
> >> wrong with this, or what I should be doing instead.
> >
> > Oh, ok.  I figured out one thing.  I have to push an AugmentedNode on
> > the stack before I start parsing, and then I take the result as the
> > "left" property of that top node.  Is that all I should have figured
> > out?  Is there a better way to do this in the first place?
> >
> <snip/>
>
> Add this as the first rule, then the parse method will get you the
> actual (root) node:
>
>   digester.addRule("node", new ObjectCreateRule(AugmentedNode.class));
>
> This calls out the root as different from other nodes -- the root
> doesn't need a SetNextRule (thats the one causing the NPE since it has
> no parent).

I'm still not quite sure how to transform my existing code so I can use the 
top node, not the "left" of the top node.

My existing code is this:

        digester.push(new AugmentedNode());

        digester.addRule("*/node", new 
ObjectCreateRule(AugmentedNode.class));
        digester.addRule("*/node", new SetPropertiesRule());
        digester.addRule("*/node", new SetNextRule("addChild", 
AugmentedNode.class.getName()));

        return ((Node) digester.parse(new StringReader(xml))).getLeft();

I've tried several variations with adding that line you suggest, but I can't 
get it to work, and I'm not sure exactly what it's trying to accomplish.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message