You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Thyr <Th...@compuserve.de> on 2004/04/14 00:40:50 UTC

[digester] addSetRoot and wildcards

Hello,

I want to use digester to solve the following task:

test.xml
<?xml version='1.0' encoding='utf-8'?>
<root>
    <a>content</a>
    <b>
       <a>deeper content</a>    
    </b>   
    <a>another content</a>
</root>

The code:
Digester d = new Digester(); 
d.addObjectCreate("root", Root.class); 
//match all "a"
d.addSetRoot("*/a", "addA", "java.lang.String");
Root root = (Root) d.parse(new File("test.xml"));

and:
public class Root {
  private String aA;

  public void addA(Object a) {
   System.out.println(a);
  }
}

The system.out tells me, the passed object is an Root-Object. But I need
to have the content.
Where is my mistake. What can I do?

Thanks for any advice! 

Reidar 


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


Re: [digester] addSetRoot and wildcards

Posted by Thyr <Th...@compuserve.de>.
Much Thanks!
I added the rule as you told me:
 d.addObjectCreate("*/a", A.class);
And additional
 d.addCallMethod("*/a", "setAA", 0);
The argument "java.lang.String" for the addSetRule is not longer needed.

Now it works fine.
Kind Regards
Reidar

-----Ursprüngliche Nachricht-----
On Wed, 2004-04-14 at 10:40, Thyr wrote:
> Hello,
> 
> I want to use digester to solve the following task:
> 
> test.xml
> <?xml version='1.0' encoding='utf-8'?>
> <root>
>     <a>content</a>
>     <b>
>        <a>deeper content</a>    
>     </b>   
>     <a>another content</a>
> </root>
> 
> The code:
> Digester d = new Digester();
> d.addObjectCreate("root", Root.class); 
> //match all "a"
> d.addSetRoot("*/a", "addA", "java.lang.String");
> Root root = (Root) d.parse(new File("test.xml"));
> 
> and:
> public class Root {
>   private String aA;
> 
>   public void addA(Object a) {
>    System.out.println(a);
>   }
> }
> 
> The system.out tells me, the passed object is an Root-Object. But I 
> need to have the content. Where is my mistake. What can I do?

You need another ObjectCreate rule, to create an instance of "A":
  digester.addObjectCreate("*/a", A.class);

Without this, the object stack is only ever 1 deep (the Root instance),
and the SetRoot rule then tries to pass the top object (a Root instance)
to the bottom object on the stack (itself!).

Regards,

Simon


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


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


Re: [digester] addSetRoot and wildcards

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Wed, 2004-04-14 at 10:40, Thyr wrote:
> Hello,
> 
> I want to use digester to solve the following task:
> 
> test.xml
> <?xml version='1.0' encoding='utf-8'?>
> <root>
>     <a>content</a>
>     <b>
>        <a>deeper content</a>    
>     </b>   
>     <a>another content</a>
> </root>
> 
> The code:
> Digester d = new Digester(); 
> d.addObjectCreate("root", Root.class); 
> //match all "a"
> d.addSetRoot("*/a", "addA", "java.lang.String");
> Root root = (Root) d.parse(new File("test.xml"));
> 
> and:
> public class Root {
>   private String aA;
> 
>   public void addA(Object a) {
>    System.out.println(a);
>   }
> }
> 
> The system.out tells me, the passed object is an Root-Object. But I need
> to have the content.
> Where is my mistake. What can I do?

You need another ObjectCreate rule, to create an instance of "A":
  digester.addObjectCreate("*/a", A.class);

Without this, the object stack is only ever 1 deep (the Root instance),
and the SetRoot rule then tries to pass the top object (a Root instance)
to the bottom object on the stack (itself!).

Regards,

Simon


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