1

here is my simple code:

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

I got this exception

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

but I don't know what does it means.where is the mistake?

1
  • this kind of exceptions raised if you trying to add second root element into exicted dom tree. Commented Jul 12, 2012 at 9:53

1 Answer 1

1

These two lines

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

cause the error. The first sets the root, the second dies it again.

Edit

Do

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

or

Document doc = new Document(performances)
Sign up to request clarification or add additional context in comments.

2 Comments

so the two lines are the same?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.