SimpleXML枚举区分大小写 [英] SimpleXML enum case-sensitivity

查看:126
本文介绍了SimpleXML枚举区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用simplexml库(v2.6.2)创建一个XML
http ://simple.sourceforge.net/home.php

I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php

我想创建的XML必须持有一个枚举值,这应该是区分大小写的。以下是POJO:

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

}

以下是序列化程序代码: / p>

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

    }

}

结果输出:

<myEnum>NEW</myEnum>

所需的输出:

<myEnum>new</myEnum>

我该怎么办?我无法更改枚举中的变量名称,因为它恰好是关键字new!

How should I proceed ? I cannot change the variable name in the enum as it happens to be the keyword "new" !

谢谢。

推荐答案

在对源代码进行了一些调查之后,我发现库使用接口 Transform 将值转换为字符串。枚举转换的默认行为由类 EnumTransform 定义。为了自定义,您可以定义自己的Transform类。以下版本的 Transform 实现将调用 toString()而不是默认的 name()枚举对象的

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

变换对象通过 Matcher 接口的对象从 match 方法返回。可能有几个 Matcher 对象。库会一个接一个地尝试,直到找到一个返回一个非空的 Transformer 对象。您可以定义自己的 Matcher 对象,并将其作为参数传递给 Persister 类的构造函数。该对象将获得最高优先级。

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

最后,您不会忘记在枚举类上定义一个toString方法。那么上面代码的组合将使你的工作使用它们的toString值来编码枚举对象。

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.

这篇关于SimpleXML枚举区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆