【问题标题】:Reading list values using xstream使用 xstream 读取列表值
【发布时间】:2012-09-22 12:01:02
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您的 XML 格式与您的 Java 对象模型不匹配。根据 XML,&lt;Property&gt;&lt;Objects&gt; 的子级,但根据您的代码,Properties 列表是 ResponseObject 的一部分。您需要修复这种不匹配。

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


    更新:

    我看到你已经修复了你的 XML。问题是你的ResponseObject 中有一个Value 字段,但是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);
         }
    }
    

    【讨论】:

    • 我已经考虑了您关于仅使用注释的第二条评论。让我更清楚地解释我的情况。 OBJECTS 标签可以有 n 个 OBJECT TYPE 标签,每个 OBJECT TYPE 标签可以有 n 个 PROPERTY 标签。
    • 所以我用这样的java类建模。 ParentResponseObject 包含一个 ResponseObject 列表,每个 ResponseObject 都包含一个 Properties 对象列表。我看到 ResponseObject 列表已正确填充,但 ResponseObject 中的属性列表却没有,这对我来说似乎很奇怪。我也无法控制 xml。我可以通过哪种方式更改java代码以实现我的目的。
    • 对此的任何帮助都非常感谢。
    • 您的 XML 对我来说看起来不正确。你能修好吗? 的孩子还是 的孩子?
    • 你好...这是xml结构,每个OBJECTS可以有n个OBJECT TYPE作为直接子代。所以我将我相应的java类建模为ParentResponseObject,它可以有一个OBJECT列表。这工作正常。 Similary OBJECT TYPE 可以有 n 个 PROPERTY 作为直接子级。所以我已经建模了 ResponseObject
    猜你喜欢
    • 1970-01-01
    • 2012-09-24
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多