I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php
The XML I want to create has to hold an enum value, which should be case-sensitive. Following is the POJO :
package pojos;
public enum MyEnum {
NEW("new"),
OLD("old");
private final String value;
MyEnum(String v)
{
value = v;
}
public String value() {
return value;
}
public static MyEnum fromValue(String v) {
for (MyEnum c: MyEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
Following is the serializer code :
import java.io.File;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import pojos.MyEnum;
public class TestEnum {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Serializer serializer = new Persister();
MyEnum example = MyEnum.NEW;
File result = new File("myenum.xml");
serializer.write(example, result);
}
}
The resultant output :
<myEnum>NEW</myEnum>
The desired output :
<myEnum>new</myEnum>
How should I proceed ? I cannot change the variable name in the enum as it happens to be the keyword "new" !
Thanks.
After some investigation of the source code, i have discovered that the library uses interface Transform
to transform values to Strings. The default behavior of enum transformations is defined by class EnumTransform
. In order to customize that, you can define you own Transform class. The following version of Transform
implementation would call toString()
instead of the default name()
on the enum objects.
class MyEnumTransform implements Transform<Enum> {
private final Class type;
public MyEnumTransform(Class type) {
this.type = type;
}
public Enum read(String value) throws Exception {
for (Object o : type.getEnumConstants()) {
if (o.toString().equals(value)) {
return (Enum) o;
}
}
return null;
}
public String write(Enum value) throws Exception {
return value.toString();
}
}
Transform
objects are returned from match
method by objects of Matcher
interface. There could be several Matcher
objects. The library tries them one by one until it finds one that returns a non-null Transformer
object. You can define your own Matcher
object and pass it as argument to the constructor of Persister
class. This object will get the highest priority.
Persister serializer = new Persister(new Matcher() {
public Transform match(Class type) throws Exception {
if (type.isEnum()) {
return new MyEnumTransform(type);
}
return null;
}
});
Finally, you wont forget to define a toString method on your enum classes. Then the combination of codes above will do you the work of encoding enum objects using their toString values.
You should override toString()
@Override
public String toString() {
return this.value.toLowerCase();
}
Then write results using
serializer.write(example.toString(), result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With