1

これが私の簡単なコードです:

try
                {
                    Element performances = new Element("performances");
                    Document doc = new Document(performances);
                    doc.setRootElement(performances);

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

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

                    XMLOutputter xmlOutput = new XMLOutputter();

                    xmlOutput.setFormat(Format.getPrettyFormat());
                    xmlOutput.output(doc, new FileWriter("/homa/mazzy/Scrivania/perfor_"+dateFormat.format(cal.getTime())+"xml"));

この例外が発生しました

Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalAddException: The element "performances" could not be added as the root of the document: The Content already has an existing parent document

しかし、それが何を意味するのかわかりません。間違いはどこにありますか?

4

1 に答える 1

1

この2行

Document doc = new Document(performances);
doc.setRootElement(performances);

エラーの原因となります。1 つ目はルートを設定し、2 つ目はそれを再び死にます。

編集

行う

Document doc = new Document()
doc.setRootElement(performances)

また

Document doc = new Document(performances)
于 2012-07-12T09:36:26.257 に答える