TiP*_*iPo 3 java xml parsing xml-parsing simple-framework
我必须解析一个可以有两种类型的 XML 文件:
<property>
<value>Some text</value>
</property>
Run Code Online (Sandbox Code Playgroud)
和
<property>
<value>
<item id="first_id"/>
<item id="second_id"/>
<item id="third_id"/>
</value>
</property>
Run Code Online (Sandbox Code Playgroud)
我怎样才能用Java做到这一点?
我创建了一个类:
@Root(strict = false)
public class PropertyValue {
@ElementList(inline = true, required = false)
private List<ItemData> items;
@Text(required = false)
private String text;
}
Run Code Online (Sandbox Code Playgroud)
ItemData是item阶级。
但这是行不通的。该代码给了我一个例外:
org.simpleframework.xml.core.TextException: Text annotation @org.simpleframework.xml.Text(data=false, empty=, required=false) on field 'text' private java.lang.String PropertyValue.text used with elements in class PropertyValue
Run Code Online (Sandbox Code Playgroud)
小智 5
我使用 ElementList 和 Entry 做到了
@ElementList(entry = "item", inline = true)
无需自定义转换器即可为我工作。全班:
@Root(name = "property")
public class Property {
@ElementList(entry = "item", inline = true, required = false)
private List<Item> items;
@Text(required = false)
private String text;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2591 次 |
| 最近记录: |