使用xstream读取列表值

时间:2012-10-01 15:48:45

标签: java xml xml-parsing xml-serialization xstream

我正在使用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 List。但是,ResponseObject中的属性列表始终为null,即使我在两种情况下都使用相同的技术。任何人都可以帮助解决这个问题。对此的帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

您的XML格式与Java对象模型不匹配。根据XML,<Property><Objects>的孩子,但根据您的代码,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);
     }
}