gpt4 book ai didi

java - 使用 xstream 读取列表值

转载 作者:行者123 更新时间:2023-11-30 11:40:30 28 4
gpt4 key购买 nike

我正在使用 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>

基本上在 Objects 标签下可以有 n 个 Object Type,每个 Object Type 可以有 n 个 Property 标签。所以我用 Java 类和代码建模,如下所示 --

class ParentResponseObject {    
List <ResponseObject>responseObjects = new ArrayList<ResponseObject>();

}

@XStreamAlias("Object")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
class ResponseObject {
String Type;
String Value;

List <Properties> properties = new ArrayList<Properties>();
}

@XStreamAlias("Property")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
class Properties {
String Name;
String Type;
String Value;
}
public class MyAgainTest {
public static void main (String[] args) throws Exception {
String k1 = //collect the xml as string
XStream s = new XStream(new DomDriver());
s.alias("Objects", ParentResponseObject.class);
s.alias("Object", ResponseObject.class);
s.alias("Property", Properties.class);
s.useAttributeFor(ResponseObject.class, "Type");
s.addImplicitCollection(ParentResponseObject.class, "responseObjects");
s.addImplicitCollection(ResponseObject.class, "properties");
s.useAttributeFor(Properties.class, "Name");
s.useAttributeFor(Properties.class, "Type");
s.processAnnotations(ParentResponseObject.class);
ParentResponseObject gh =(ParentResponseObject)s.fromXML(k1);
System.out.println(gh.toString());
}
}

使用此代码,我能够在 ParentResponseObject 类中填充 responseObjects 列表。但是,ResponseObject 中的属性列表始终为空,即使我在这两种情况下使用相同的技术。任何人都可以帮助解决这个问题。非常感谢对此提供帮助。

最佳答案

您的 XML 格式与您的 Java 对象模型不匹配。根据 XML,<Property><Objects> 的 child ,但根据您的代码,Properties列表是 ResponseObject 的一部分.您需要解决此不匹配问题。

此外,您似乎混合使用了注释和代码。要么只使用注释(推荐),要么全部在代码中完成。否则,您的代码会变得困惑和不可读。


更新:

我看到您已经修复了 XML。问题是你有一个 Value在你的字段 ResponseObject , 但 xml 元素中没有值,因此将其删除。

下面的代码应该可以工作:

@XStreamAlias("Objects")
public class ParentResponseObject {

@XStreamImplicit
List<ResponseObject> responseObjects = new ArrayList<ResponseObject>();
}

@XStreamAlias("Object")
public class ResponseObject {

@XStreamAsAttribute
String Type;

@XStreamImplicit
List<Properties> properties = new ArrayList<Properties>();
}

@XStreamAlias("Property")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
public class Properties {
String Name;
String Type;
String Value;
}

主要方法:

XStream s = new XStream(new DomDriver());
s.processAnnotations(ParentResponseObject.class);
ParentResponseObject gh = (ParentResponseObject) s.fromXML(xml);

for (ResponseObject o : gh.responseObjects) {
System.out.println(o.Type);
for (Properties p : o.properties) {
System.out.println(p.Name + ":" + p.Type + ":" + p.Value);
}
}

关于java - 使用 xstream 读取列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12676638/

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