防止在ElementListUnion中为空的ElementList包含空标记 [英] Prevent inclusion of empty tag for an empty ElementList in an ElementListUnion

查看:65
本文介绍了防止在ElementListUnion中为空的ElementList包含空标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个班上有以下代码:

I have the following code in one of my classes:

@Text(required=false)
@ElementListUnion({
    @ElementList(required = false, inline = true, type = com.company.Child.class, entry="values")
})
public List<Object> valueUnion;

请注意,这似乎是使框架与既包含子元素又包含文本的元素一起使用的唯一方法.当存在文本且elementlist也包含元素并生成以下xml时,此方法效果很好:

Note that this seems to be the only way to get the framework to work with elements that contain both children and text. This works great when text is present and the elementlist contains elements as well, and produces the following xml:

<parent>
    <values>val 1</values>
    <values>val 2</values>
    some text
</parent>

但是,有时元素列表不包含任何元素,仅包含文本(这意味着valueUnion List仅包含一个元素,即文本字符串).但是,这将导致以下XML:

However, sometimes the element list contains no elements, with only the text being present (meaning the valueUnion List only contains one element, the string of text). This however, results in the following XML:

<parent>
    <values />
    some text
</parent>

问题就在这里,因为这会导致服务器阻塞空的< values/> 标记.不幸的是,我无法控制服务器上的代码,并且我正在寻找一种方法,如果elementlist不包含任何元素,则强制Simple忽略空标签.

And herein lies the problem, as this causes the server to choke over the empty <values /> tag. Unfortunately I do not have control over the code on the server, and I am looking for a way to force Simple to ignore the empty tag if the elementlist contains no elements.

推荐答案

我为您提供了一种解决方法.它不是很漂亮,但是这个概念可以在这里为您提供帮助.
可以使用自定义的 Converter 来序列化对象,而不是使用元素注释.

I have a workaround for you. Its not beautiful, but the concept can help you here.
Instead of element-annotations you can use a custom Converter which serializes your Objects.

  • 示例类:

(包含列表,文本和您需要的其他元素)

@Root(name="parent")
@Convert(ExampleConverter.class)
public class Example
{
    private String text; // Save the text you want to set ('some text' in your code)
    private List<Object> valueUnion;

    // Constructor + getter / setter
}

实际上,这里您只需要 @Convert(ExampleConverter.class) @Root 批注,因为序列化是在您自己的转换器中完成的( ExampleConverter ).

Actually you only need the @Convert(ExampleConverter.class) and @Root Annotations here, since the serialization is done in your own converter (ExampleConverter).

  • ExampleConverter 类:

(在此处序列化/反序列化您的对象)

public class ExampleConverter implements Converter
{
    @Override
    public Object read(InputNode node) throws Exception
    {
        /* TODO: Deserialize your class here (if required). */
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public void write(OutputNode node, Object value) throws Exception
    {
        final Example val = (Example) value;
        final List<Object> l = val.getValueUnion();

        if( !l.isEmpty() ) // if there are elements, insert their nodes
        {
            for( Object obj : l )
            {
                node.getChild("values").setValue(obj.toString());
            }
        }
        else
        {
            node.getChild("values").setValue(""); // this creates <values></values> if list is empty
        }
        node.setValue(val.getText()); // Set the text (1)
    } 
}

(1):即使有其他元素,这也会设置您的文本.但是,此解决方案可能会破坏您的格式.文本和结束标记将在同一行上.您可以通过插入换行来解决此问题.

(1): This will set your text even if there are some other elements. However this solution may break your formating; the text and the closing tag will be on the same line. You can solve this by inserting a new line.

  • 用法:

创建一个序列化程序,制定您的策略并进行读写

Create a serializer, et your strategy and write / read

Serializer ser = new Persister(new AnnotationStrategy()); // 'AnnotationStrategy is important here!
ser.write(...); // write / read

  • 示例:
  • 列表中包含元素:

    Example t = new Example();
    t.setText("abc"); // Set the text
    t.getValueUnion().add("value1"); // Add some elements to list
    t.getValueUnion().add("value2");
    
    Serializer ser = new Persister(new AnnotationStrategy());
    ser.write(t, f); // 'f' is the file to write
    

    输出:

    <parent>
       <values>value1</values>
       <values>value2</values>abc</parent>
    

    列表中没有元素:

    Example t = new Example();
    t.setText("abc"); // Set the text
    
    Serializer ser = new Persister(new AnnotationStrategy());
    ser.write(t, f); // 'f' is the file to write
    

    输出:

    <parent>
       <values></values>abc</parent>
    

    请注意前面所述的格式问题"!

    这篇关于防止在ElementListUnion中为空的ElementList包含空标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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