Java 简单 XML 转换器将元素写入 OutputNode

Java Simple XML Converter write element to OutputNode

简单 XML 允许您制作自己的转换器。

This write method is used to serialize an object to XML. The serialization should be performed in such a way that all of the objects values are represented by an element or attribute of the provided node. This ensures that it can be fully deserialized at a later time.

@Convert(MyClass.Converter.class)
public class MyClass {
  public MyClass() {}

  public static class Converter implements Converter<MyClass> {
    public MyClass read(InputNode node) {
      return new MyClass();
    }

    public void write(OutputNode node, MyClass value) {

    }
  }
}

文档描述了在 OutputNode 中表示一个元素,那么如何向 OutputNode 添加一个元素? OutputNode 的文档似乎没有任何向其添加元素或节点的方法。

您可以手动添加元素,也可以使用另一个 Serializer 添加它 -- 序列化程序也可以 write/read to/from 个节点。

这是一个例子:

@Root(name = "MyClass") // @Root required
@Convert(MyClass.MyClassConverter.class)
public class MyClass
{
    private int value;
    private String name;

    /* Ctor, getter, setter etc. */


    public static class MyClassConverter implements Converter<MyClass>
    {
        @Override
        public MyClass read(InputNode node) throws Exception
        {
            MyClass mc = new MyClass();

            /* Read the (int) value of 'someValue' node */
            int value = Integer.parseInt(node.getNext("someValue").getValue());
            mc.setValue(value);

            /* Same for the string */
            String name = node.getNext("someString_" + value).getValue();
            mc.setName(name);

            /* Do something with data not used in MyClass, but useable anyway */
            if( node.getNext("from") == null )
            {
                throw new IllegalArgumentException("Node 'from' is missing!");
            }

            return mc;
        }


        @Override
        public void write(OutputNode node, MyClass value) throws Exception
        {
            /* Add an attribute to the root node */
            node.setAttribute("example", "true");

            OutputNode valueNode = node.getChild("someValue");      // Create a child node ...
            valueNode.setAttribute("stack", "overflow");            // ... with an attribute
            valueNode.setValue(String.valueOf(value.getValue()));   // ... and a value

            /*
             * Converter allow a dynamic creation -- here the node names is
             * created from two values of MyClass
             */
            node.getChild("someString_" + value.getValue())     // Create another child node ...
                    .setValue(value.getName());                 // ... with a value only

            /* Create another node from scratch */
            OutputNode fromNode = node.getChild("from");
            fromNode.setValue("scratch");
            fromNode.setComment("This node is created by the converter");
        }

    }

}

xml写/读:

<MyClass example="true">
   <someValue stack="overflow">27</someValue>
   <someString_27>abc</someString_27>
   <!-- This node is created by the converter -->
   <from>scratch</from>
</MyClass>

重要:你必须使用AnnotationStrategy,否则@Convert将无法工作。

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