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

我在使用无法更改的特定格式的SimpleFramework反序列化xml时有以下情况…

 THIS INNER TEXT IS THE ISSUE  45.7 42.7   45.7 42.7    

这是我为Question写的课程

 @Root (name="Question") public class Question { @Attribute (name="ID") private String id; @ElementList (inline=true, required=false) private List criteria; @Text private String text; // And their getter and setters... } 

现在的问题是, 我无法获得内部文字……

任何人都可以建议我这样做…… ???

你不能在这里使用@Text注释,这只有在你没有孩子的情况下才有可能。

并且它[ @Text注释]不能与另一个XML元素注释一起出现,例如Element注释。

来源: @Text API文档

但是,您可以使用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 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; 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 { 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 ,它允许您一起使用文本子元素 。 您可以使用Serializer来编写所有Criteria -childs。

有一些toString()方法,仅用于测试 – 您可以根据需要实现它们。

输入XML:

 This inner text ...  45.7 42.7   45.7 42.7   

示例代码:

 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对象。

Interesting Posts