You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Fr...@genedata.com on 2004/04/13 07:33:29 UTC

[Digester] - how to Parse Tag attributes and tag content with addObjectCreate and CallMethod...

I am trying to set up the parse rules for an xml entry of the following 
type: 
<Root> 
  <subSection> 
    <id>7</id>	 
      <Item Name="forProperty" Type="String">This is the 
Content</Item>  
      <Item Name="forAnotherProperty" Type="String">More 
Content</Item>  
  </subSection> 
</Root> 
I want to achieve the equivalent of SubSection.setForProperty 
with the value  "This is the Content" and 
SubSection.setAnotherProperty with "More  Content". 
How do I do this? 

Thanks. 





Re: [Digester] - how to Parse Tag attributes and tag content with addObjectCreate and CallMethod...

Posted by Fr...@genedata.com.
Thanks for the feedback.

Unfortunately, there is not a lot I can do about the XML, which is from a public site.

I have solved the problem, but not quite with the mechanisms you suggest.

For the XML:
<Root> 
  <subSection> 
    <id>7</id>	 
      <Item Name="forProperty" Type="String">This is the 
Content</Item>  
      <Item Name="forAnotherProperty" Type="String">More 
Content</Item>  
  </subSection> 
</Root> 

I used this:

	digester.addObjectCreate("root", root.class);
	digester.addObjectCreate("root/subsection",subsection.class);
	digester.addBeanPropertySetter("root/subsection/Id", "id");
	digester.addObjectCreate("root/subsection/Item", Item.class); 
	digester.addSetProperties("root/subsection/Item", "Name", "name");
	digester.addSetProperties("root/subsection/Item", "Type", "type");
	digester.addSetNext("root/subsection/Item", "addItem" );
	digester.addCallMethod("root/subsection/Item", "addItemValue", 0); 
	digester.addSetNext("root/subsection", "addSubsection");

which seems to do the job of parsing, so that I have a root of subsections, each 
subsection containing items; each item has a list of 'properties' [name] with values 
corresponding to the tag 'content'.

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


Re: [Digester] - how to Parse Tag attributes and tag content with addObjectCreate and CallMethod...

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2004-04-13 at 18:07, Simon Kitching wrote:
> <Root> 
> >   <subSection> 
> >     <id>7</id>	 
> >       <Item Name="forProperty" Type="String">This is the 
> > Content</Item>  
> >       <Item Name="forAnotherProperty" Type="String">More 
> > Content</Item>  
> >   </subSection> 
> > </Root> 
> 
> Or the SetNestedPropertiesRule (which is currently only available in
> nightly builds) can be used to do this:
>   <forProperty Value="This is the context"/>

umm .. actually, SetNestedPropertiesRule takes data like this:

  <subSection>
    <id>7</id>
    <forProperty>This is the context</forProperty>
    <forAnotherProperty>More Content</forAnotherProperty>
  </subSection>

What's most embarrassing about this mistake is - I wrote the
SetNestedPropertiesRule class. I need to see a doctor about my
alzheimers right away :-).


Regards,

Simon



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


Re: [Digester] - how to Parse Tag attributes and tag content with addObjectCreate and CallMethod...

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
Hi,

On Tue, 2004-04-13 at 17:33, Francis.LeMonnier@genedata.com wrote:
> I am trying to set up the parse rules for an xml entry of the following 
> type: 
> <Root> 
>   <subSection> 
>     <id>7</id>	 
>       <Item Name="forProperty" Type="String">This is the 
> Content</Item>  
>       <Item Name="forAnotherProperty" Type="String">More 
> Content</Item>  
>   </subSection> 
> </Root> 
> I want to achieve the equivalent of SubSection.setForProperty 
> with the value  "This is the Content" and 
> SubSection.setAnotherProperty with "More  Content". 
> How do I do this? 

I believe you'll have to write your own rule. What you need is a variant
of the SetPropertyRule.

SetPropertyRule is very similar to what you want, except that it takes
data like this:
  <Item Name="forProperty" Value="This is the context"/>

If you can change your input xml to be like SetPropertyRule expects,
that would probably be a good idea.

Or the SetNestedPropertiesRule (which is currently only available in
nightly builds) can be used to do this:
  <forProperty Value="This is the context"/>

Note that neither take a "type" parameter; they try to automatically
convert the input string to the appropriate type for the target bean
property.

If you are determined to use the format you describe in your email, then
it isn't that big a deal to write your own Rule class. I can provide you
with some hints on that if you need them.

Regards,

Simon



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