Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

[jdom-interest] Creating new XML documents from existing XML
 document(s)

robert w2 phillips

2005-07-28

Replies:

Hello,

I am trying to create a new XML document from an existing XML document using JDOM, but am unable to do so. I want to take the root element of an existing document and use it as the root element of a new XML document. The code I am using is as follows:

--------------------------------

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import java.io.StringReader;

public class JDOMTest
{
  public static void main(String[] args) throws JDOMException {
    mytest();
  }

  public static void mytest() {
    try {
       // XML String. Elements from this will form the basis of the new XML document
       String xmlString="<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
       SAXBuilder builder = new SAXBuilder();
       // Extract the root element from the XML string
       Document test = builder.build(new StringReader(xmlString));
       Element root = test.getRootElement();

       // Create a new XML document and set its root element
       Document newDocument = new Document();
       newDocument.setRootElement(root);
       
       System.out.println("Complete!");
    }
    catch (Exception e) {
       System.out.println("General Exception ocurred : "+e);
    }
  }
}

------------------------------

This compiles without error, but every time I run it I get the following error:

------------------------------
> java JDOMTest
General Exception ocurred : org.jdom.IllegalAddException: The element "note" could not be added as the root of the document: The Content already has an existing parent document
------------------------------

Why will this not work? The problem occurs because I am trying to create two "org.jdom.Document" objects, but this works in pre 1.0 JDOM libraries. Is there is a restriction on the number of Document objects I can create, or have I missed something obvious?

I have tried compiling it and running it on two different systems with two different version of Java, but both give the same result. On Windows XP SP2 I am using:
------------------------------
java version "1.4.2_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)
------------------------------

On UNIX, I am using:
------------------------------
java version "1.4.1_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_03-b02)
Java HotSpot(TM) Client VM (build 1.4.1_03-b02, mixed mode)
------------------------------

I built the JDOM.JAR library from the latest download available off the website (1.0). As I mentioned, older (pre-1.0 release) JDOM.JAR libraries work OK.

Any help you can offer would be most appreciated.

Best regards,

Rob.

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.