2012-12-11 80 views
0

我有一个xml文件,根元素中有多个数组。无论何时试图使用隐式集合声明,xstream只处理最后一个声明,其他所有内容都保持为空。在下面的代码中,第一个数组ERRORS保留为空,而DEBITS被解析。Xstream解析多个列表

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; 
    } 

} 

回答

0

我不太清楚你期望它做什么。 XStream是一个bean/XML映射工具,实际上你有两个XML列表映射到bean中相同的List元素。如果你真的想要这个XML结构体,我会使用SAX解析器或类似的方式构造你的bean,创建一个空列表并让解析器回调添加到该列表中。