我正在使用Xstream
以下格式读取一些 xml
<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类和代码来建模如下
@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;
}
}
使用此代码,我可以responseObjects
在类中填充列表ParentResponseObject
。但是,即使我在这两种情况下都使用相同的技术,列表中的属性ResponseObject
总是并且不会被填充。null
我调试了很多,但找不到任何东西。征求您的帮助和指导。