0

私は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があり、各ObjectTypeにはn個のPropertyタグがあります。だから私は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たくさんデバッグしましたが、何も見つかりませんでした。あなたの助けと指導が求められます。

4

1 に答える 1

2

動作する暗黙の参照を追加します

  @XStreamImplicit(itemFieldName="Object")
  List<ResponseObject> responseObjects = new ArrayList<ResponseObject>();

  @XStreamImplicit(itemFieldName="Property")
  List<Properties> properties = new ArrayList<Properties>();
于 2012-10-03T09:27:00.490 に答える