2

変更できない特定の形式のSimpleFrameworkを使用してxmlを逆シリアル化する際に、次のような状況があります...

<Question ID="Q1">
    THIS INNER TEXT IS THE ISSUE

    <Criteria Type="Normal" Source="OEM">
        <Value Type="0">45.7</Value>
        <Value Type="100">42.7</Value>
    </Criteria>
    <Criteria Type="Impact" Source="OEM">
        <Value Type="0">45.7</Value>
        <Value Type="100">42.7</Value>
    </Criteria>
    <!-- CRITERIA CAN HAVE ANY NUMBER -->

</Question>

ここに私が質問のために書いたクラスがあります

@Root (name="Question")
public class Question {

    @Attribute (name="ID") 
    private String id;

    @ElementList (inline=true, required=false)
    private List<Criteria> criteria;

    @Text
    private String text;

    // And their getter and setters...
}

問題は、内部テキストを取得できないことです...

誰かが私にこれを行う方法を提案できますか...???

4

1 に答える 1

2

ここではアノテーションを使用できません@Text。これは、子がいない場合にのみ可能です。

[@Text注釈] 注釈など、別の XML 要素の注釈と共に表示することはできませんElement

出典: @TextAPI ドキュメント

ただし、Converterこれらのテキストには a を使用できます。これは少し難しいですが、例を次に示します。

Criteriaクラス:

@Root(name = "Criteria")
public class Criteria
{
    @Attribute(name = "Type")
    private String type;
    @Attribute(name = "Source")
    private String source;
    @ElementList(name = "Values", inline = true)
    private ArrayList<Value> values;



    public Criteria(String type, String source)
    {
        this.type = type;
        this.source = source;
        this.values = new ArrayList<>();
    }

    private Criteria() { }


    // ...


    @Override
    public String toString()
    {
        return "Criteria{" + "type=" + type + ", source=" + source + ", values=" + values + '}';
    }


    // Inner class for values - you also can use a normal one instead
    @Root(name = "Value")
    public static class Value
    {
        @Attribute(name = "Type", required = true)
        private int type;
        @Text(required = true)
        private double value;


        public Value(int type, double value)
        {
            this.type = type;
            this.value = value;
        }

        private Value() { }

    } 

}

Questionクラス:

@Root(name = "Question")
@Convert( value = Question.QuestionConvert.class)
public class Question
{
    @Attribute(name = "ID", required = true)
    private String id;
    @Element(name = "text")
    private String text;
    @ElementList(inline = true)
    private ArrayList<Criteria> criteria;


    public Question(String id)
    {
        this.id = id;
        this.criteria = new ArrayList<>();

        this.text = "This inner text ...";
    }

    private Question() { }


    // ...


    @Override
    public String toString()
    {
        return "Question{" + "id=" + id + ", text=" + text + ", criteria=" + criteria + '}';
    }



    static class QuestionConvert implements Converter<Question>
    {
        private final Serializer ser = new Persister();


        @Override
        public Question read(InputNode node) throws Exception
        {
            Question q = new Question();
            q.id = node.getAttribute("ID").getValue();
            q.text = node.getValue();

            q.criteria = new ArrayList<>();
            InputNode criteria = node.getNext("Criteria");

            while( criteria != null )
            {
                q.criteria.add(ser.read(Criteria.class, criteria));
                criteria = node.getNext("Criteria");
            }

            return q;
        }


        @Override
        public void write(OutputNode node, Question value) throws Exception
        {
            node.setAttribute("ID", value.id);
            node.setValue(value.text);


            for( Criteria c : value.getCriteria() )
            {
                ser.write(c, node);
            }
        }
    }
}

これらすべての空のコンストラクターに注意してください。それらは単純に必要ですが、非公開にすることができます。これらの内部クラスを内部として実装する必要はありません。

解決策の鍵は、テキスト子要素を一緒Converterに使用できるようにすることです。a を使用して、すべての-childs を書き込むことができます。SerializerCriteria

いくつかのtoString()方法がありますが、それらはテスト専用です。必要に応じて実装できます。

入力 XML:

<Question ID="Q1">This inner text ...
   <Criteria Type="Normal" Source="OEM">
      <Value Type="0">45.7</Value>
      <Value Type="100">42.7</Value>
   </Criteria>
   <Criteria Type="Impact" Source="OEM">
      <Value Type="0">45.7</Value>
      <Value Type="100">42.7</Value>
   </Criteria>
</Question>

コード例:

Serializer ser = new Persister(new AnnotationStrategy()); // Don't miss the AnnotationStrategy!

Question q = ser.read(Question.class, f);
System.out.println(q);

出力:

Question{id=Q1, text=This inner text ...
   , criteria=[Criteria{type=Normal, source=OEM, values=[Value{type=0, value=45.7}, Value{type=100, value=42.7}]}, Criteria{type=Impact, source=OEM, values=[Value{type=0, value=45.7}, Value{type=100, value=42.7}]}]}

あまり美しくはありませんが、機能しています!:-)

Ps。Converter の両方のメソッドが実装されているため、このコードを使用してオブジェクトをシリアル化することもできQuestionます。

于 2013-07-05T20:48:20.260 に答える