如何解析自定义用户的标签?

How to parse custom user's tags?

有注释 @ElementMap,允许将此类标签解析为地图:

<field key="key">value</field>

是否可以将此类标签解析为地图?:

<key>value</key>

我尝试为我的 class 编写自定义转换器,但它仍然不允许在 xml 中使用自定义用户的标签。

I tried to write custom converter for my class, but it still does not allow to use custom user's tags in xml.

你能提供更多细节吗?


这是一个如何实现转换器的示例:

@Root
@Convert(Example.ExampleConverter.class)
public class Example
{
    private Map<String, String> map;

    // ...

    static class ExampleConverter implements Converter<Example>
    {
        @Override
        public Example read(InputNode node) throws Exception
        {
            Example value = new Example();
            value.map = new HashMap<>();

            InputNode childNode = node.getNext();

            while( childNode != null )
            {
                value.map.put(childNode.getName(), childNode.getValue());
                childNode = node.getNext();
            }

            return value;
        }

        @Override
        public void write(OutputNode node, Example value) throws Exception
        {
            for( Entry<String, String> entry : value.map.entrySet() )
            {
                node.getChild(entry.getKey()).setValue(entry.getValue());
            }
        }
    }
}

用法示例:

Serializer ser = new Persister(new AnnotationStrategy());

final String xml = "<example>\n"
        + "   <key1>value1</key1>\n"
        + "   <key2>value2</key2>\n"
        + "   <key3>value3</key3>\n"
        + "</example>";

Example e = ser.read(Example.class, xml);
System.out.println(e);

输出(取决于toString()-实现:

Example{map={key1=value1, key2=value2, key3=value3}}