Another way to handle this kind of problem is either just to have your own
Map of Action objects and you could look up the right Action object based on
the element name.

Or you could try using the org.dom4j.rule package which allows you to attach
arbitrary processing to a document then use declarative XSLT style matching
to invoke custom Actions on a kind of node. e.g.

// lets define a stylesheet...
Stylesheet stylesheet = new Stylesheet();
Rule rule = new Rule();
rule.setPattern(
    DocumentHelper.createPattern( "foo" )
);
rule.setAction(
    new Action() {
        public void run(Node node) {
            ... do something
        }
    }
);
stylesheet.addRule(rule);

rule = new Rule();
rule.setPattern(
    DocumentHelper.createPattern( "bar[@x='123']" )
);
rule.setAction(
    new Action() {
        public void run(Node node) {
            ... do something
        }
    }
);
stylesheet.addRule(rule);


then when you have a node or document you can evaluate the stylesheet via...

Node node = ...;
stylesheet.run(node);

The advantage of using the above is you can declaratively walk a document or
document fragment, in the same way XSLT does, processing each node in a well
defined way, using XSLT pattern matching

James
----- Original Message -----
From: "Brain, Jim" <[EMAIL PROTECTED]>
To: "DOM4J Mailing List (E-mail)" <[EMAIL PROTECTED]>
Sent: Tuesday, July 16, 2002 9:37 PM
Subject: [dom4j-user] A quick question


> I have a lot of this:
>
>             Iterator i=el.elementIterator();
>             while(i.hasNext()) {
>                 item=(Element)i.next();
>                 name = item.getName();
>                 if (name.equals("General")) {
>                     addGeneralInfo(item);
>                 } else if (name.equals("Payment")) {
>                     //addPaymentInfo(item);
>                 } else if (name.equals("Financial")) {
>                     //addFinancialInfo(item);
>                 } else if (name.equals("Beneficiary")) {
>                     //addBeneficiary(item);
>                 } else if (name.equals("Coverage")) {
>                     //addCoverage(item);
>                 } else if (name.equals("SubAccount")) {
>                     //addAccount(item);
>                 } else if (name.equals("ServiceRequestData")) {
>                     // dunno...
>                 } else if (name.equals("ErrorMessage")) {
>                     String error=item.getTextTrim();
>                     if(!error.equals("")) {
>                         throw new ProcessException(100,error);
>                         //throw new ProcessException(2102,"Policy not
found.
> Invalid Policy and/or Company Number");
>                     }
>                 }
>             }
>
> Is there a better way to do this?  I don't want to use Xpath all the time
> (as it is expensive), but was wondering if others had come up with a
better
> way of doing this that does not involve the big if statement.
>
> Jim
>
>
> Jim Brain, [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> "Researching tomorrow's decisions today."
> (319) 369-2070 (work)
> Systems Architect, ITS, AEGON Financial Partners
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.comm


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to