2012-07-13 56 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