Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用xml输出程序创建xml文件时出现异常_Java_Xml - Fatal编程技术网

Java 使用xml输出程序创建xml文件时出现异常

Java 使用xml输出程序创建xml文件时出现异常,java,xml,Java,Xml,以下是我的简单代码: try { Element performances = new Element("performances"); Document doc = new Document(performances); doc.setRootElement(performances); perform

以下是我的简单代码:

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
但是我不知道这是什么意思。哪里错了?

这两行

Document doc = new Document(performances);
doc.setRootElement(performances);
造成错误。第一个设定根,第二个设定根,第二个设定根

编辑

这两条线

Document doc = new Document(performances);
doc.setRootElement(performances);
造成错误。第一个设定根,第二个设定根,第二个设定根

编辑


所以这两行是相同的?请看,这两行是相同的?请看,如果您试图将第二个根元素添加到现有dom树中,则会引发此类异常。我的回答解决了您的问题吗?如果您试图将第二个根元素添加到现有dom树中,则会引发此类异常。我的回答解决了您的问题吗?
Document doc = new Document(performances)