JDOM Error

View: New views
2 Messages — Rating Filter:   Alert me  

JDOM Error

by shekher thakur :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/youraddr@...

Re: JDOM Error

by Laurent Bihanic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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/youraddr@...