commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Reid Pinchback <>
Subject Re: [digester] ref another part of xml
Date Wed, 10 Nov 2004 00:00:31 GMT

I've run into this one before.  It is relatively easy
as long as you are willing to make a simplifying
assumption: a reference can only happen AFTER the real
thing, not before it.  That way you will always
have the real thing recorded for lookup when you
hit the reference.  Things are tougher if you
can't make such an assumption.  In that case you
need a lazy proxy to represent the references,
and after processing all the data you need to
do a second pass to convert the references.

I added a "data" attribute in the example below
that wasn't supplied for the references.  When
the result is printed, you only see data on a
reference if digesting succesfully matched the
reference to the real object.  The output should

That second "bob,x2" is the result of resolving the

import java.util.*;
import org.apache.commons.digester.*;
import org.xml.sax.*;

public class PersonExample3 {

  static Map repository=new HashMap();
  public static class Person {
    String id;
    String data;
    List personList = new ArrayList();
    public void addPerson(Person p){
    public void setId(String id) {; }
    public String getId() { return id; }
    public void setData(String data) {;
    public String getData() { return data; }
    public String toString() { 
      String result="Person["+id+","+data+"]";
      if (personList.size() > 0) { result+=personList;
      return result;

  public static class PersonFactory extends
AbstractObjectCreationFactory {
    public Object createObject(Attributes attributes)
throws Exception {
      String id=attributes.getValue("id");
      if (null!=id) {
        Person p=new Person();;"data");
        repository.put(id, p);
        return p;
      String refId=attributes.getValue("ref-id");
      Person p=(Person)repository.get(refId);
      return p;

  public static class Root {
    Person adamOrEve;
    public void addPerson(Person p){

  Digester makeConfiguredDigester() {
    Digester digester=new Digester();
    digester.addFactoryCreate("*/person", new
    digester.addSetNext("*/person", "addPerson");
    return digester;

  public static void main(String[] args) throws
Exception {
    String input="<root><person id='sue'
data='x1'><person id='bob' data='x2'/><person id='joe'
    PersonExample3 ex=new PersonExample3();
    Digester digester=ex.makeConfiguredDigester();
    Root root=new Root();
    digester.parse(new StringReader(input));


--- Steve Blom <> wrote:

> Hi,
> First off, thxs so much for your help Reid! But, yet
> another question :-)  Would it be possible to have a
> reference pointer attribute to grab an object from
> outside that element. Hard to put in words, but
> hopefully you can catch on with the example.
> <person id="Jon">
>  ...
> </person>
> <person id="Sue">
>   <person-ref-id="Jon"/>
>   ...
> </person>
> Now, when when i come across the family-member, I
> want
> to use the person-ref-id to grab the person object
> that is described earlier and set it. Is this
> possible
> with Digester? If so, any restrictions(Jon has to
> come
> before sue in xml). I need to be able to describe a
> person within a person, or have a person-ref that
> points to another person element in the xml. Kinda
> the
> same as
> <person id="Sue">
>   <person id="Jon">
>     ...
>   </person>
> </person>
> Any suggestions on the best way to do this?
> Thanks - Steve
> __________________________________ 
> Do you Yahoo!? 
> Check out the new Yahoo! Front Page. 
> To unsubscribe, e-mail:
> For additional commands, e-mail:

Do you Yahoo!? 
Check out the new Yahoo! Front Page. 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message