1

我有一个用xmloutputter. 在第一次创建时创建的 xml 文件,它为我创建了标头,但随后我在其上附加了信息,我不会标头。我怎么能省略?

编辑

            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));
4

2 回答 2

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)。

于 2012-07-14T20:53:24.707 回答
0

您是否尝试过在 Format 上使用此方法,然后在 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);

给出<performances />没有 XML 声明的标准输出。

于 2012-07-14T19:44:52.193 回答