Re: JDOM Error

Laurent Bihanic <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
A JDOM Element can be associated with one and only one parent (Element or 
Document).
As you put the "Document doc = new Document(root);" statement inside the first 
level loop, you attempt to attach the Fibonacci_Numbers element to many documents.

I guess you should close both loops before creating & outputting the document.
Othewise, call root.detach() before creating the new document.

Laurent


shekher thakur a écrit :
> 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.