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

Paul Cooper paul_premis at cooperscoop.com
Thu Jul 28 13:39:21 PDT 2005


Rob,

It all depends on what you want to do with the original document once you
create the new one. Kevin's approach is safer in that it leaves the original
document intact (so no side effects). I actually considered sending the same
answer he did....(though I would have recommended cloning the root element,
I didn't realize the document was clonable). It also depends on how large
the document is, since cloning it would probably double the in-memory
footprint.

Someone please correct me if I'm wrong about the memory footprint....

Paul 

-----Original Message-----
From: robert.w2.phillips at bt.com [mailto:robert.w2.phillips at bt.com] 
Sent: Thursday, July 28, 2005 4:01 PM
To: kevin.cobb at emergint.com; paul_premis at cooperscoop.com;
jdom-interest at jdom.org
Subject: RE: [jdom-interest] Creating new XML documents from
existingXMLdocument(s)

Hi Kevin,

I think you're both right.

This code is a snippet from a larger piece of software that I've recently
taken over development of, and so I'm not sure myself whether the original
developer intended to keep the original document, or lose it altogether.

I'm not in the office at the moment so I will try both your and Paul's
suggestions tomorrow morning and let you know how it goes.

Thanks very much for all your help!

Rob.


-----Original Message-----
From:	Kevin L. Cobb [mailto:kevin.cobb at emergint.com]
Sent:	Thu 7/28/2005 8:48 PM
To:	Paul Cooper; Phillips,RW,Robert,MLC366 R; jdom-interest at jdom.org
Cc:	
Subject:	RE: [jdom-interest] Creating new XML documents from
existingXMLdocument(s)
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