2

私は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クラスとそれを読むためのコードで次のようにモデル化しました-

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のプロパティリストは常にnullです。誰かがこれを解決するのを手伝ってくれませんか。これに関するヘルプは高く評価されています。

4

1 に答える 1

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);
     }
}
于 2012-10-01T16:11:44.763 に答える