gpt4 book ai didi

java - 简单框架 XML : Element with Inner Text and Child Elements

转载 作者:行者123 更新时间:2023-11-30 09:20:21 25 4
gpt4 key购买 nike

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

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

谁能建议我这样做的方法...???

最佳答案

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

and it [@Text annotation] can not appear with the another XML element annotations, such as the Element annotation.

来源:@Text API documentation

但是,您可以对这些文本使用转换器。这有点棘手,但这是一个示例:

条件类:

@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() { }

}

}

问题类:

@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);
}
}
}
}

请注意所有那些空的构造函数。它们是 simple 所必需的,但您可以将它们保密。您不必将这些内部类实现为内部类。

解决方案的关键是 Converter,它允许您一起使用 textchild-elements。您可以使用 Serializer 编写所有 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}]}]}

不是很漂亮,但是很管用! :-)

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

关于java - 简单框架 XML : Element with Inner Text and Child Elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com