commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: read java.util.Properties from XML with digester
Date Mon, 18 Aug 2008 08:20:04 GMT
mnereson schrieb:
> Anyone know how to read XML content to java.util.Properties?
>
> Input would be something like
>
> <root>
>   <properties>
>     <propertynameA>value</propertynameA>
>     <propertynameB>value</propertynameB>
>     <propertynameC>value</propertynameC> 
>   <properties>
> </root>
>
> I need to have a Properties entity with  three keys (propertynameA,
> propertynameB, propertynameC) and three values (value, value value)
>
> I can figure out as much as
>
> final Digester digester = new Digester();
> digester.addObjectCreate("root/properties", Properties.class);
> ... << what goes here to add keys and set values?
>
> final StringReader reader = new StringReader(xml);
> Properties properties = (Properties) digester.parse(reader);
>
> But I am not sure how to grab arbitrary elements under "root/properties" and
> assign them as a Property key.
>   

Digester isn't really designed to process elements whose names are not 
fixed. The whole concept of "rules" matching elements really only works 
when the elements have names that are always the same.

However there are a couple of cases where non-constant names can be 
supported, and I think your example xml above can be processed. I suggest:
* first configure the digester to use the ExtendedBaseRules class as its 
"rules engine"
* then you can use the pattern "root/properties/*" to execute your own 
custom Rule class
* your custom rule will do its work in the method
     public void body(namespace, name, text)
   so the name of the element is easily available.

That's not too complicated, but not entirely trivial either. If your 
input really is as simple as the example above then maybe you should 
simply parse the input using the SAX api directly, and not bother with 
using Digester.

Regards,
Simon


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


Mime
View raw message