[jdom-interest] Divorcing Root Element from document!!

Richard Landon RLandon at youbet.com
Wed Nov 8 18:08:51 PST 2000

We have accomplished a similiar thing to these threads using JDOM beta-4.
Here's how (but first some background)

(0) Our system presents a stateless session EJB as a front-end for receiving
    statements/documents via a publish-subscribe mechanism
(1) This EJB takes the XML input as String via an business method, processes
    XML contents, decomposing the XML into transactional operations (ie
    EJB invocations) thus updating our persistent store (we use CMP Entity
EJB for this
    but that a side-note, really). The appropriate EJB invocations return
values objects,
    that is model entities, which can on request represent themselves as XML
(string) data.

(2) This EJB also changes the output XML (mutates the JDOM document) as a
side effect
    and at the end of this process the business methods passed back the
modified XML
    data also a String.

Here how we deal with it using JDOM (and we like the solution alot)

(A) First note that the Element type in JDOM implements the Java interface
Clonable! (is this the secret trick?)
(B) Suppose then the following Java pseudo-code:

    Element elementInDocument=..;// An element somwhere within an existing
    Document newDocument=...;    // The new document (created via a
Builder.build(...) call)
    Element tempRoot=newDocument.getRootElement(); // OKAY!
    List nodes=tempRoot.getChildren(); // Fetches the List of Elements off
the root element of the new document
    Element node = null; Element clonedNode = null; // Some working
    Iterator i = nodes.iterator();	 
    while (i.hasNext())  {
     node = (Element)i.next();	
     clonedNode = (Element)node.clone();	// Please see Element.java
(clone() method for details....)	  	

(C) This code should effectively clone all the children below a given
element with one document, as the 
    children of another element within some other document. You should be
able to generalize this to
    fit your case?

Hope that helps, Richard Landon


-----Original Message-----
From: Alex Wang [mailto:awang at niku.com]
Sent: Tuesday, November 07, 2000 3:18 PM
To: jdom-interest at jdom.org
Subject: RE: [jdom-interest] Divorcing Root Element from document!!

I was trying to do things differently: I wanted to merge two or more XML
documents which conform the same DTD so that my root JDOM element (from the
1st file) will have all children elements of root elements from all files.
Is there an easy way of doing it?

I am running JDOM beta 4.


-----Original Message-----
From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of bob mcwhirter
Sent: Tuesday, November 07, 2000 1:32 PM
To: royd at tsainc.com
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Divorcing Root Element from document!!

> I am trying to take the root element of one Document and append it as a
> child element on a second document... I continually get errors about it
> having a parent.  How do you divorce the root element from the Document?

Set the source Document's RootElement to null, methinks, after
you've suck out the RootElement that needs transplanting.

	Element root = doc.getRootElement();

	// can now reparent root


To control your jdom-interest membership:

To control your jdom-interest membership:

More information about the jdom-interest mailing list