2012-07-13 60 views
1

我有xmloutputter .AT則創建它創建了我的頭,但後來我追加它的信息在第一時間創建一個XML文件,我會不header.how能我省略了?xml文件中如何omitt頭部附加信息時

編輯

  Element performances = new Element("performances"); 
      Document doc = new Document(performances); 

      performances.setAttribute(new Attribute("date", dateFormat.format(cal.getTime()))); 

      //Uptime 
      Element uptime = new Element("uptime"); 
      uptime.addContent(new Element("days").setText(new Long(duptime).toString())); 
      uptime.addContent(new Element("hours").setText(new Long(huptime).toString())); 
      uptime.addContent(new Element("minutes").setText(new Long(muptime).toString())); 
      uptime.addContent(new Element("seconds").setText(new Long(suptime).toString())); 
      doc.getRootElement().addContent(uptime); 
      //TotalHitsCount 
      doc.getRootElement().addContent(new Element("totalhitcount").setText(new Long(stats.gettotal_hit_count()).toString())); 
      //EmittedBytes 
      doc.getRootElement().addContent(new Element("emittedbytes").setText(new Long(stats.getemitted_bytes()).toString())); 
      //Avghitsec 
      doc.getRootElement().addContent(new Element("avghitsec").setText(new Float(stats.getavg_hit_sec()).toString())); 
      //Avgbyteshit 
      doc.getRootElement().addContent(new Element("avgbyteshit").setText(new Long(stats.getavgbytes_hit()).toString())); 
      //Avgbps 
      doc.getRootElement().addContent(new Element("avgbps").setText(new Long(stats.getavgbps()).toString())); 
      //Total threads 
      doc.getRootElement().addContent(new Element("totalthreads").setText(new Long(stats.gettotal_threads()).toString())); 
      //Idle threads 
      doc.getRootElement().addContent(new Element("idlethreads").setText(new Long(stats.getidle_threads()).toString())); 

      XMLOutputter xmlOutput = new XMLOutputter(); 

      xmlOutput.setFormat(Format.getPrettyFormat()); 
      xmlOutput.output(doc, new FileWriter((String)path+"performances.xml",true)); 
+0

什麼是xmloutputter?它似乎不是JDK中的一個類。 – 2012-07-14 18:58:39

+0

它屬於jdom包 – Mazzy 2012-07-14 19:25:54

回答

2

你不能做你想做的事情,因爲XML格式不允許它。一個xml文檔有一個單個的根級標記。即使你忽略了xml頭文件(這對於大多數xml工具來說肯定是可行的),你的xml文件將是無效的(我假設你需要從標準xml解析器中讀取這個文件)。

這些文件看起來是這樣的:

<?xml version="1.0"?> 
<performances> 
    <update/> 
    <!-- ... more xml elements ... --> 
</performances> 
<performances> 
    <update/> 
    <!-- ... appended xml elements ... --> 
</performances> 

,這是不是有效的XML文件。

爲了「附加」到xml文件,您必須讀取現有文件,並用其他節點重新編寫它完全。一個簡單的方法是使用DocumentBuilder將現有文件讀入DOM,將新節點添加到現有的根元素,然後重新寫入整個xml文件。

如果你真正需要以追加的方式,寫入數據的話,我建議你使用其他比XML(如某種簡單的分隔的平面文件,如CSV)的文件格式。

0

您是否嘗試過這個使用這個method on Format,然後這個constructor for XMLOutputter

Element performances = new Element("performances"); 
    Document doc = new Document(performances); 
    XMLOutputter xmlOutput = new XMLOutputter(); 

    Format format = Format.getPrettyFormat(); 
    format.setOmitDeclaration(true); 
    xmlOutput.setFormat(format); 

    StringWriter writer = new StringWriter(); 
    xmlOutput.output(doc, writer); 
    System.out.println(writer); 

在沒有XML聲明的標準輸出中給出了<performances />

+0

是的,我嘗試過,但我無法在格式對象上調用該方法...上面我的代碼 – Mazzy 2012-07-14 19:57:51