【问题标题】:Simple XML and HashSet简单的 XML 和 HashSet
【发布时间】:2013-12-30 16:14:24
【问题描述】:

我正在发现 Simple XML 并尝试序列化这个简单的类:

public class Div
{
  private Set< String > _classes = new HashSet< String >() {{
    add( "a" );
    add( "b" );
    add( "c" );
  }};

  // some methods and attributes...
}

收件人:

<div class="a b c">
</div>

有@Attribute 注释,但这不能将集合转换为字符串。简单 XML 提供了一些“转换器”来完成这项工作,但我找不到任何示例。

谢谢

【问题讨论】:

    标签: java xml serialization simple-framework


    【解决方案1】:

    最好使用Converter,而不是转换器。您可以在一个类以及单个字段上使用它们。基本上一个Converter就是“如何将带注释的对象翻译成xml节点”的实现

    下面是一个示例,使用 Converter 表示类 Div

    @Root(name = "div")
    @Convert(value = Div.DivConverter.class) // Set the Converter for this class
    public class Div
    {
        private Set< String> _classes = new HashSet< String>()
        {
            {
                add("a");
                add("b");
                add("c");
            }
        };
    
        // some methods and attributes...
    
        public Set<String> getClasses()
        {
            return _classes;
        }
    
        // ...
    
    
        /*
         * Converter implementation.
         */
        static class DivConverter implements Converter<Div>
        {
            @Override
            public Div read(InputNode node) throws Exception
            {
                /*
                 * Not required for writing, therefore not implemented int this
                 * example.
                 */
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
    
    
            @Override
            public void write(OutputNode node, Div value) throws Exception
            {
                StringBuilder sb = new StringBuilder(); // Used to store the set's values in string-form
                Set<String> cl = value.getClasses();
    
                if( cl.isEmpty() == false ) // Check if classes is empty - nothing to do if so
                {
                    int pos = 0;    // Keep trac of current position (not to add an blank char for last entry)
                    final int size = cl.size();
    
                    for( String s : cl )    // Iterate over all entries of classes
                    {
                        sb.append(s);   // Append the entry to buffer
    
                        if( (pos++) < size - 1 )
                        {
                            sb.append(' '); // If not last entry, add a blank
                        }
                    }
                }
    
                // Finally add the attribute 'class' with the content, to the 'div'-node
                node.setAttribute("class", sb.toString());
            }
        }
    }
    

    注意:转换器也可以实现为普通类,我在本示例中使用了内部类,以将所有内容保持在一起。

    使用方法:

    Div d = new Div();
    
    File f = new File("test.xml");
    
    Serializer ser = new Persister(new AnnotationStrategy()); /* Don't miss AnnotationStrategy!! */
    ser.write(d, f);
    

    对于反序列化,只需实现 Converter 的 read() 方法。要将属性值返回到集合,请使用 StringTokenizer

    【讨论】:

    • 顺便说一句。您也可以在您的转换器中使用 Serializer / Persister within;如果您的类中有许多其他字段,则可以使用序列化程序而不是在每个字段上手动执行。
    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多