3

クラスの1つに次のコードがあります。

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

子とテキストの両方を含む要素をフレームワークで処理するには、これが唯一の方法のように思われることに注意してください。これは、テキストが存在し、要素リストにも要素が含まれている場合にうまく機能し、次の xml を生成します。

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

ただし、要素リストに要素が含まれておらず、テキストのみが存在する場合があります (つまり、valueUnion リストには、テキストの文字列という 1 つの要素のみが含まれます)。ただし、これは次の XML になります。

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

そして、ここに問題があります。これにより、サーバーが空の<values />タグを詰まらせてしまうからです。残念ながら、サーバー上のコードを制御することはできません。要素リストに要素が含まれていない場合、Simple に空のタグを強制的に無視させる方法を探しています。

4

1 に答える 1

2

回避策があります。それは美しくはありませんが、コンセプトはここであなたを助けることができます。要素アノテーションの代わりに、オブジェクトをシリアル
化するカスタムを使用できます。Converter

  • Exampleクラス:

(リスト、テキスト、その他の必要な要素が含まれています)

@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)と注釈のみが必要です。@RootExampleConverter

  • 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):他の要素がある場合でもテキストを設定します。ただし、このソリューションはフォーマットを壊す可能性があります。テキストと終了タグは同じ行にあります。これは、新しい行を挿入することで解決できます。

  • 使用法:

シリアライザーを作成し、戦略を立てて書き込み/読み取り

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>

前述のフォーマットの「問題」に注意してください。

于 2013-01-22T12:21:43.433 に答える