使用 xstream 填充列表值 [英] Populating list values using xstream
问题描述
我正在使用 Xstream
以下列格式读取一些 xml
I am working with Xstream
to read some xml in the following format
<Objects>
<Object Type="System.Management.Automation.Internal.Host.InternalHost">
<Property Name="Name" Type="System.String">ConsoleHost</Property>
<Property Name="Version" Type="System.Version">2.0</Property>
<Property Name="InstanceId" Type="System.Guid">7e2156</Property>
</Object>
</Objects>
基本上在对象标签下可以有 n 个对象类型,每个对象类型可以有 n 个属性标签.所以我用Java类和代码来建模,如下
Basically under Objects tag there can be n number of Object Type and each Object Type can have n number of Property tags. So I have modelled by Java classes and the code to read it as follows
@XStreamAlias("Objects")
class ParentResponseObject {
@XStreamImplicit
List <ResponseObject>responseObjects = new ArrayList<ResponseObject>();
public String toString () {
return responseObjects.get(0).toString();
}
}
@XStreamAlias("Object")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
class ResponseObject {
@XStreamAsAttribute
String Type;
String value;
@XStreamImplicit
List <Properties> properties = new ArrayList<Properties>();
public String toString () {
return Type+" value is "+"List is "+properties+ value;
}
}
@XStreamAlias("Property")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
class Properties {
@XStreamAsAttribute
String Name;
@XStreamAsAttribute
String Type;
String value;
Properties (String name, String type,String value) {
this.Name = name;
this.Type = type;
this.value = value;
}
}
使用此代码,我可以填充 ParentResponseObject
类中的 responseObjects
列表.但是,ResponseObject
中的属性 List 始终为 null
并且不会被填充,即使我在这两种情况下都使用相同的技术.我已经调试了很多,但找不到任何东西.恳请您的帮助和指导.
Using this code, I am able to populate the responseObjects
List in the ParentResponseObject
class. However, the properties List in the ResponseObject
is always null
and is not getting populated , even though I am using the same technique in both the cases. I have debugged a lot but could not find anything. Your help and guidance is solicited.
推荐答案
添加对隐式工作的引用
@XStreamImplicit(itemFieldName="Object")
List<ResponseObject> responseObjects = new ArrayList<ResponseObject>();
@XStreamImplicit(itemFieldName="Property")
List<Properties> properties = new ArrayList<Properties>();
这篇关于使用 xstream 填充列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!