On Sun, 2006-07-30 at 17:16 -0400, rjn wrote:
> Hey Everyone,
> 
> Thank you for the responses to my previous e-mail.  I see that that
> NodeCreateRule would work.
> 
> However, I'm wondering how to use it.  This is my first time parsing
> XML, so please excuse my ignorance.
> 
> So, here's what I have:
> 
>               Digester d = new Digester();
>               
>               d.push(this);
>               
>               d.addCallMethod("feed/entry", "addEntry", 2);
>               d.addCallParam("feed/entry/title", 0);
>               d.addCallParam("feed/entry/content/div", 1);

It looks like you've got a "factory" method that creates entries:
  public void addEntry(String title, String div) {
    Entry e = new Entry(title, div);
    this.entries.add(e);
  }

You could rewrite like this:
  public void addEntry(String title, Node div) {
    String divText = ....; // serialize node to string
    Entry e = new Entry(title, divText);
    this.entries.add(e);
  }
in which case you need these rules:
   // creates a dom Node representing the div and its content, and
   // push it onto the object stack
   d.addNodeCreate("feed/entry/content/div");
   // set param #1 of the method call to the top object on the stack
   d.addCallParam("feed/entry/content/div", 1, 0);

However I would recommend instead that you create the Entry object from
an ObjectCreateRule. Your main class therefore has:
  public void addEntry(Entry e) {
    this.entries.add(e);
  }
and the Entry class is:
  public class Entry {
    private String title;
    private String divText;

    public void setDiv(Node div) {
      divText = ...; // serialize node to string
    }

    // other getters, setters, etc
  }
with:
  // for each entry tag, create an Entry object and push it onto
  // the digester stack. Also pass this object to the addEntry
  // method of the preceding object on the stack.
  d.addObjectCreate("feed/entry", Entry.class);
  d.addSetNext("feed/entry", "addEntry");

  // when the title tag is found, call setTitle on the top object
  // on the stack (ie the Entry object created by the earlier
  // ObjectCreateRule.
  d.addCallMethod("feed/entry/title", "setTitle", 0);

  // create a dom NODE and push it onto the stack, then pass
  // it to the setDiv method of the parent object on the
  // stack (the Entry object)
  d.addNodeCreate("feed/entry/content/div");
  d.addSetNext("feed/entry/content/div", "setDiv");


Of course this is all untested...

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to