【问题标题】:Java Simple XML Converter write element to OutputNodeJava Simple XML Converter 将元素写入 OutputNode
【发布时间】:2015-10-02 09:58:33
【问题描述】:

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

这个 write 方法用于将对象序列化为 XML。这 序列化应该以这样的方式执行,即所有 对象值由一个元素或属性表示 提供节点。这确保了它可以在一个完全反序列化 稍后。

@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 的文档似乎没有任何方法可以向其添加元素或节点。

【问题讨论】:

    标签: java xml-parsing simple-framework


    【解决方案1】:

    您可以手动添加元素,也可以使用另一个Serializer 来添加它——Serializer 还可以向节点写入/读取/从节点读取。

    这是一个例子:

    @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(...);
    

    【讨论】:

    • 太好了!我完全忽略了 getChild 上的 javadoc。谢谢。
    猜你喜欢
    • 2017-07-20
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多