Java – simplexml enumerations are case sensitive

I've been trying to create XML using the simplexml Library (v2.6.2)

The XML I want to create must save the enumeration value, which should be case sensitive The following are POJOs:

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);
        }

}

The following is the serialization 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);

    }

}

Result output:

<myEnum>NEW</myEnum>

Required output:

<myEnum>new</myEnum>

What should I do? I can't change the variable name in the enumeration because it happens to be the keyword "new"!

thank you.

Solution

After some investigation of the source code, I found that the library uses the interface transform to convert values to strings The default behavior of enumeration transformation is defined by the class enumtransform To customize it, you can define your own transform class The following versions of the transform implementation will call toString () instead of the default name () on the enumeration object

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();
    }
}

The conversion object is returned by the matching method through the object of the matcher interface There may be several matcher objects The library tries them one by one until it finds a transformer object that returns non null You can define your own matcher object and pass it as a parameter to the constructor of the persister class This object will receive 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 won't forget to define the toString method on the enumeration class Then the above code combination will use their toString value to encode the work of enumerating objects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>