SimpleFramwork XML:具有内部文本和子元素的元素

时间:2013-07-04 06:06:48

标签: java xml xml-serialization simple-framework innertext

我在使用无法更改的特定格式的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>

这是我为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...
}

现在的问题是,我无法获取内部文字......

有人可以建议我这样做...... ???

1 个答案:

答案 0 :(得分:2)

您不能在此处使用@Text注释,只有在您没有孩子的情况下才能使用此注释。

  

并且[@Text注释]不能与其他XML元素注释一起出现,例如   作为Element注释。

来源:@Text API documentation

但是,您可以对这些文字使用Converter。这有点棘手,但这是一个例子:

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,它允许您一起使用 text 子元素。您可以使用Serializer来编写所有Criteria - 孩子。

有一些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}]}]}

不是很漂亮,但它正在发挥作用! : - )

聚苯乙烯。由于实现了Converter的两种方法,您还可以使用此代码序列化Question对象。