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