Java 使用xstream读取列表值

Java 使用xstream读取列表值,java,xml,xml-parsing,xml-serialization,xstream,Java,Xml,Xml Parsing,Xml Serialization,Xstream,我正在使用xstream阅读以下格式的xml-- 控制台主机 2 7e2156 基本上,在对象标记下可以有n个对象类型,每个对象类型可以有n个属性标记。因此,我用Java类和代码对其进行建模,如下所示-- 类ParentResponseObject{ List responseObjects=new ArrayList(); } @XStreamAlias(“对象”) @XStreamConverter(value=ToAttributedValueConverter.class,strin

我正在使用
xstream
阅读以下格式的xml--


控制台主机
2
7e2156
基本上,在对象标记下可以有n个对象类型,每个对象类型可以有n个属性标记。因此,我用Java类和代码对其进行建模,如下所示--

类ParentResponseObject{
List responseObjects=new ArrayList();
}
@XStreamAlias(“对象”)
@XStreamConverter(value=ToAttributedValueConverter.class,strings={“value”})
类响应对象{
字符串类型;
字符串值;
列表属性=新的ArrayList();
}
@XStreamAlias(“财产”)
@XStreamConverter(value=ToAttributedValueConverter.class,strings={“value”})
类属性{
字符串名;
字符串类型;
字符串值;
}
公共类Myagainest{
公共静态void main(字符串[]args)引发异常{
String k1=//将xml收集为字符串
XStream s=newxstream(newdomdriver());
s、 别名(“对象”,ParentResponseObject.class);
s、 别名(“对象”,ResponseObject.class);
s、 别名(“Property”,Properties.class);
s、 使用attributef(ResponseObject.class,“Type”);
s、 addImplicitCollection(ParentResponseObject.class,“responseObject”);
s、 addImplicitCollection(ResponseObject.class,“属性”);
s、 使用AttributeFor(Properties.class,“Name”);
s、 使用attributeFor(Properties.class,“Type”);
s、 processAnnotations(ParentResponseObject.class);
ParentResponseObject gh=(ParentResponseObject)s.fromXML(k1);
System.out.println(gh.toString());
}
}

使用这段代码,我能够在ParentResponseObject类中填充ResponseObject列表。但是,ResponseObject中的属性列表始终为空,即使我在这两种情况下使用相同的技术。谁能帮我解决这个问题。非常感谢您的帮助。

您的XML格式与Java对象模型不匹配。根据XML,
的子对象,但根据您的代码,
属性
列表是
响应对象
的一部分。您需要修复此不匹配

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


更新:

我看您已经修复了XML。问题是在
响应对象
中有一个
字段,但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;
}

我已经考虑了您关于只使用注释的第二个评论。让我更清楚地解释我的情况。OBJECTS标记可以有n个对象类型标记,每个对象类型标记可以有n个属性标记。ParentResponseObject包含一个ResponseObject列表,每个ResponseObject包含一个Properties对象列表。我看到ResponseObject列表填充正确,但ResponseObject中的属性列表填充不正确,这对我来说很奇怪。而且我无法控制xml。我可以通过哪种方式更改java代码来达到我的目的。非常感谢您在这方面提供的任何帮助。我认为您的XML不正确。你能修一下吗?是的子对象还是的子对象?您好……这是xml结构,每个对象可以有n个对象类型作为直接子对象。因此,我将对应的java类建模为ParentResponseObject,它可以有一个对象列表。这很好用。类似的对象类型可以有n个属性作为直接子对象
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());
    }
}
@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);
     }
}