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

Kevin L. Cobb kevin.cobb at emergint.com
Thu Jul 28 12:48:09 PDT 2005


Be careful! By detaching it you lose your original document (test)
contents. If that's OK, then detatch is a better way to go.  

-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of Paul Cooper
Sent: Thursday, July 28, 2005 3:12 PM
To: robert.w2.phillips at bt.com; jdom-interest at jdom.org
Subject: RE: [jdom-interest] Creating new XML documents from
existingXMLdocument(s)

You can detach the root element from the original document and add it to
the new document.

// Create a new XML document and set its root element Document
newDocument = new Document(); newDocument.setRootElement(root.detach());


-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org]
On Behalf Of robert.w2.phillips at bt.com
Sent: Thursday, July 28, 2005 2:23 PM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Creating new XML documents from existing
XMLdocument(s)

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>Reminde
r</h
eading><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@yourhost.com


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com





More information about the jdom-interest mailing list