2014-02-11 92 views
0

我試圖創建XML報告,可以打開爲xls表。 我有以下的輸出:Xstream - 更改ArrayList元素名稱

<Report> 
    <test> 
    <string>4.419</string> 
    <string>4.256</string> 
    </test> 
</Report> 

從這個代碼:

/** 
* declare arrays 
*/ 
// ArrayList<String> test = new ArrayList<String>(); 
ArrayList<String> stats = new ArrayList<String>(); 
// ArrayList<String> count = new ArrayList<String>(); 

/** 
*return array list with loading times 
*/ 
public ArrayList launch() { 

    for (int i = 0; i < 2; i++) { 

    //  ui.off(); 
    //  ui.on(); 
    device.pressHome(); 
    ui.openProgramInMenu("ON"); 

    long TStart = System.currentTimeMillis(); 
    ui.detectContactList(); 
    long TStop = System.currentTimeMillis(); 
    float res = TStop - TStart; 
    res /= 1000; 
    ui.log("[loading time]: " + res); 
    //  ui.off(); 

    test.add(i, "Loading time"); 
    stats.add(i, Float.toString(res)); 
    count.add(i, Integer.toString(i)); 
    } 
    System.out.println(stats); 
} 

其中rep.class具有代碼:

public class ReportSettings { 

    public List<String> test = new ArrayList<String>(); 
    public List<String> count = new ArrayList<String>(); 
    public List<String> stats = new ArrayList<String>(); 

     /** 
     * Test method 
     */ 
    public static void main(String[] args) { 
     ReportSettings rep = new ReportSettings(); 
     rep.saveXML("report/data.xml"); 
     //  System.out.println(rep.test); 
     //  rep = rep.loadXML("report/data.xml"); 
     //  System.out.println(rep.home); 

     System.out.println(rep.getXML()); 
    } 

    public void createReport() { 
     ReportSettings rep = new ReportSettings(); 
     rep.saveXML("report/data.xml"); 
    } 

    public String getXML() { 
     XStream xstream = new XStream(); 
     xstream.alias("Report", ReportSettings.class); 
     xstream.autodetectAnnotations(true); 
     return xstream.toXML(this); 
    } 

    public void saveXML(String filename) { 
     if (!filename.contains(".xml")) { 
      System.out.println("Error in saveReport syntax"); 
      return; 
     } 
     String xml = this.getXML(); 
     File f = new File(filename); 
     try { 

      FileOutputStream fo = new FileOutputStream(f); 
      fo.write(xml.getBytes()); 
      fo.close(); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public ReportSettings loadXML(String filename) { 
     if (!filename.endsWith(".xml")) { 
      System.out.println("Error in loadReport syntax!"); 
      throw new RuntimeException("Error in loadReport syntax!"); 
     } 
     File f = new File(filename); 
     XStream xstream = new XStream(new DomDriver()); 
     xstream.alias("Report", ReportSettings.class); 
     xstream.autodetectAnnotations(true); 
     ReportSettings ort = (ReportSettings)xstream.fromXML(f); 
     return ort; 
    } 
} 

最後,我想從3 ArrayList中,其中{創建表統計,計數,測試} *我。/n 如何在XML文件中使用Xstream.alias將<strings>更改爲<somethingAnother>?我需要將它們更改爲stringOne和stringTwo作爲示例。

回答

0

在序列化爲XML時,您可以使用Xstream中的ClassAliasMapper爲集合中的項目提供不同的標記。

您添加這樣的塊(每個集合:統計,計算,測試):

ClassAliasingMapper statsMapper = new ClassAliasingMapper(xstream.getMapper()); 
mapper.addClassAlias("somethingAnother", String.class); 
xstream.registerLocalConverter(
    InteractionSession.class, 
    "stats", 
    new CollectionConverter(mapper) 
    );