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这些文本。这有点棘手,但这里有一个例子:

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它允许您一起使用文本子元素。您可以使用 aSerializer来编写所有的Criteria-childs。

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

不是很漂亮,但它的工作!:-)

附言。由于转换器的两种方法都已实现,因此您还可以使用此代码序列化Question对象。

于 2013-07-05T20:48:20.260 回答