You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Yawn, Mike" <mi...@hp.com> on 2005/02/17 19:45:17 UTC

Can digester give me comments from the XML?

Is there any rule pattern I can use that will allow me to process
comments from the XML file I'm parsing?
 
Background, for the inevitable 'why would you want to do that' question:
 
Originally, my configuration file (that digester processes) was intended
to be read-only.  Comments were frequently embedded in the file for
various reasons.  At some point, an enhancement was required to allow
users to update some of the configuration values, requiring that I write
the XML file back out.  So I created toXMLNode() methods in each of my
objects, and the writing out of the configuration file, if it is
changed, is completely independent of Digester.
 
The problem is, once this is done, all comments that might have been in
the file are gone.
 
I'd like to be able to add a Comment property to each configurable
object in my application, and have Digester invoke a rule on every
comment found in the config file.  I can then call a setComment property
on the object that follows, so the comment text will be preserved as
part of the object, and the toXMLNode methods can recreate the comment
text when writing the output file.  Ability to preserve all the
whitespace formatting within the comment is desired, but not essential.
 
Is this possible?
 
Thanks,
Mike
 

Re: Can digester give me comments from the XML?

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
digester doesn't support this but it shouldn't be too hard to extend
digester a little and solve your problem. 

use a parser which supports SAX optional extensions and create a
digester subclass that implements LexicalHandler. accumulate the
comments which occur in a string buffer and store on a named stack (in
the same way that body text is handled). lastly, create a custom rule
that examines and processes the named stack when end is called.

- robert

On Thu, 2005-02-17 at 18:45, Yawn, Mike wrote:
> Is there any rule pattern I can use that will allow me to process
> comments from the XML file I'm parsing?
>  
> Background, for the inevitable 'why would you want to do that' question:
>  
> Originally, my configuration file (that digester processes) was intended
> to be read-only.  Comments were frequently embedded in the file for
> various reasons.  At some point, an enhancement was required to allow
> users to update some of the configuration values, requiring that I write
> the XML file back out.  So I created toXMLNode() methods in each of my
> objects, and the writing out of the configuration file, if it is
> changed, is completely independent of Digester.
>  
> The problem is, once this is done, all comments that might have been in
> the file are gone.
>  
> I'd like to be able to add a Comment property to each configurable
> object in my application, and have Digester invoke a rule on every
> comment found in the config file.  I can then call a setComment property
> on the object that follows, so the comment text will be preserved as
> part of the object, and the toXMLNode methods can recreate the comment
> text when writing the output file.  Ability to preserve all the
> whitespace formatting within the comment is desired, but not essential.
>  
> Is this possible?
>  
> Thanks,
> Mike
>  


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