Parameters XML serialisation_124354657 - Gumtree/gumtree GitHub Wiki
Created by Tony Lam, last modified on Jul 08, 2009
GumTree provides an useful API to handling parameters or properties in XML format. Assume you want to configure your system using key-value pairs, and make them serialisable, XStream + org.gumtree.core.util.IParameters is your answer.
To use this API, you need to import from the org.gumtree.core bundle:
To serialise or deserialise using XStream, we need to configure it to support customised converter:
Say we have three key value paris as shown:
In the Java API, you can retrieve the content by:
Notice in example 1 all properties are stored in String. This is true that by default all properties are String in XML format. However, the parameters API can store any arbitrary object as value:
Retrieving the content is simple:
IParameters parameters = new Parameters(); parameters.put("some_key", "value"); parameters.put("some_more_key", new Object());
XStream xStream = new XStream(); xStream.alias("parameters", IParameters.class, Parameters.class); xStream.registerConverter(new ParametersConverter(xStream.getMapper()));
Key
Value
title
Big Title
visible
false
count
3
Equivalent XML using the parameters API will be:
<parameters> <title>Big Title</title> <visible>false</visible> <count>30</count> </parameters>
String title = parameters.getString("title"); boolean visible = parameters.get("visible", boolean.class); int count = parameters.get("visible", int.class);
<parameters>
<title>Big Title</title>
<count>
<int>30<int>
</count>
<myObject>
<org.gumtree.core.util.xml.SampleModel>
<id>id2</id>
<value>30</value>
</org.gumtree.core.util.xml.SampleModel>
</myObject>
</parameters>
String title = parameters.getString("title"); int count = parameters.get("visible", int.class); SampleModel model = parameters.get("myObject", SampleModel.class);
Document generated by Confluence on Apr 01, 2015 00:11