You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by mnereson <mi...@gmail.com> on 2008/08/18 04:12:49 UTC

read java.util.Properties from XML with digester

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.

Thank you.
-- 
View this message in context: http://www.nabble.com/read-java.util.Properties-from-XML-with-digester-tp19025271p19025271.html
Sent from the Commons - User mailing list archive at Nabble.com.


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


Re: read java.util.Properties from XML with digester

Posted by Emmanuel Bourg <eb...@apache.org>.
Simon Kitching a écrit :

> 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.

Alternatively, an XMLConfiguration from Commons Configuration would 
probably do the job. It's a bit heavier though.

Emmanuel Bourg

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


Re: read java.util.Properties from XML with digester

Posted by Simon Kitching <sk...@apache.org>.
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