JDOM Error

"shekher thakur" <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hi all,

i am using JDOM for creating XML and below is a sample code i have written
to create  XML.i am a beginer in the XML+JDOM

*

public* *class* FibonacciJDOM {

*public* *static* *void* main(String[] args) {

Element root = *new* Element("Fibonacci_Numbers");

BigInteger low = BigInteger.*ONE*;

BigInteger high = BigInteger.*ONE*;

*for*(*int* j=1; j<=11; j++){

*for* (*int* i = j; i <= 2; i++) {

Element fibonacci = *new* Element("fibonacci"+j);

fibonacci.setAttribute("index", String.*valueOf*(i));

fibonacci.setText(low.toString());

root.addContent(fibonacci);

BigInteger temp = high;

high = high.add(low);

low = temp;

}

Document doc = *new* Document(root);

// serialize it onto System.out

*try* {

XMLOutputter serializer = *new* XMLOutputter();

FileWriter writer = *new* FileWriter("c:/myFile.xml");

serializer.output(doc, writer);

 writer.close();

}

*catch* (IOException e) {

System.*err*.println(e);

}

}

}

}



But when i run my example i get the follwing error



Exception in thread "main" *org.jdom.IllegalAddException*: The element
"Fibonacci_Numbers" could not be added as the root of the document: The
Content already has an existing parent document

at org.jdom.ContentList.add(*ContentList.java:205*)

at org.jdom.ContentList.add(*ContentList.java:131*)

at java.util.AbstractList.add(*AbstractList.java:91*)

at org.jdom.Document.setRootElement(*Document.java:236*)

at org.jdom.Document.<init>(*Document.java:117*)

at org.jdom.Document.<init>(*Document.java:154*)

at test.FibonacciJDOM.main(*FibonacciJDOM.java:31*)



i am unable to find out the problem what is causing this.



any suggestion in this regard will be much helpful.



Thanks in advaive

Shekher

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.