[jdom-interest] JDOM Error

shekher thakur shekher.thakur at gmail.com
Thu Nov 27 04:58:18 PST 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20081127/0ad01728/attachment.htm


More information about the jdom-interest mailing list