Java Xstream解析多个列表
我有一个xml文件,其中根元素中有多个数组。每当我尝试使用implicitcollection声明时,xstream只处理最后一个声明,其他所有内容都保持为null。在下面的代码中,第一个数组错误保持为null,而借项被解析Java Xstream解析多个列表,java,xml-parsing,xstream,Java,Xml Parsing,Xstream,我有一个xml文件,其中根元素中有多个数组。每当我尝试使用implicitcollection声明时,xstream只处理最后一个声明,其他所有内容都保持为null。在下面的代码中,第一个数组错误保持为null,而借项被解析 public class XMLParser { /** * @param args * @throws Exception */ public static void main(String[] args){
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;
}
}
公共类XMLParser{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args){
字符串xmlString=”“+
" " +
" -1 " +
" " +
" " +
" -2 " +
" " +
" " +
" 1 " +
" " +
" " +
" 2 " +
" " +
" ";
借记WSDS借记WSDS;
试一试{
debitsWSDS=ConvertFeeSetXmlResultToDebitsWSDS(xmlString);
System.out.println(debitsWSDS.ERRORS==null);
System.out.println(debitsWSDS.DEBITS==null);
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有静态DebitsWSDS ConvertfeeSetXMLResultToDebitsWSDS(字符串xml)引发异常{
XStream XStream=newxstream(new statxdriver());
别名(“DebitsWSDS”,DebitsWSDS.class);
addImplicitCollection(DebitsWSDS.class,“DEBITS”);
addImplicitCollection(DebitsWSDS.class,“错误”);
别名(“ERROR_ID”,String.class);
//System.out.println(xml);
DebitsWSDS DebitsWSDS=(DebitsWSDS)xstream.fromXML(xml);
返回借记WSDS;
}
}
公共类借记WSDS{
公开列表错误;
公开名单借项;
公共类错误{
公共字符串错误\u ID;
}
公共类借方{
公共字符串借方ID;
}
}
我不太确定您希望它做什么。XStream是一个bean/XML映射工具,您可以有效地将两个XML列表映射到bean中相同的List
元素。如果您真的需要XML结构,我将使用SAX解析器或类似的工具构建bean,创建一个空列表,并让解析器回调添加到该列表中