我有一个 xml 文件,其中根元素中有多个数组。每当我尝试使用隐式集合声明时,xstream 只处理最后一个声明,其他一切都保持为空。在下面的代码中,第一个数组 ERRORS 保持为空,而 DEBITS 被解析。
public class XMLParser {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args){
String xmlString = "<DebitsWSDS xmlns=\"\"> " +
" <DEBITS> " +
" <DEBIT_ID>-1</DEBIT_ID> " +
" </DEBITS> " +
" <DEBITS> " +
" <DEBIT_ID>-2</DEBIT_ID> " +
" </DEBITS> " +
" <ERRORS> " +
" <ERROR_ID>1</ERROR_ID> " +
" </ERRORS> " +
" <ERRORS> " +
" <ERROR_ID>2</ERROR_ID> " +
" </ERRORS> " +
"</DebitsWSDS> ";
DebitsWSDS debitsWSDS;
try {
debitsWSDS = convertFeesSetXMLResultToDebitsWSDS(xmlString);
System.out.println(debitsWSDS.ERRORS==null);
System.out.println(debitsWSDS.DEBITS==null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static DebitsWSDS convertFeesSetXMLResultToDebitsWSDS(String xml) throws Exception{
XStream xstream = new XStream(new StaxDriver());
xstream.alias("DebitsWSDS", DebitsWSDS.class);
xstream.addImplicitCollection(DebitsWSDS.class, "DEBITS");
xstream.addImplicitCollection(DebitsWSDS.class, "ERRORS");
xstream.alias("ERROR_ID", String.class);
//System.out.println(xml);
DebitsWSDS debitsWSDS =(DebitsWSDS)xstream.fromXML(xml);
return debitsWSDS;
}
}
public class DebitsWSDS {
public List<ERROR> ERRORS;
public List<DEBIT> DEBITS;
public class ERROR {
public String ERROR_ID;
}
public class DEBIT {
public String DEBIT_ID;
}
}