Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I merge two different XML document using JDOM

Posted on 2007-11-15
3
Medium Priority
?
1,426 Views
Last Modified: 2013-11-18
Hi,

I have two XML document as listed below:

1)  (xmlDocA)

<GLogXMLElement>
<Location>
     <TransactionCode>NP</TransactionCode>
      <LocationGid>
         <Gid>
            <Xid>Rockville</Xid>
         </Gid>
      </LocationGid>
      <Address>
         <AddressLines>
            <SequenceNumber>2442</SequenceNumber>
            <AddressLine>1221 E Otem Circle</AddressLine>
         </AddressLines>
         <City>Miami</City>
         <ProvidenceCode>FL</ProvidenceCode>
         <PostalCode>91991</PostalCode>
      </Address>
</Location>
</GLogXMLElement>

2)     (xmlDocB)

        <Transmission>
      <TransmissionHeader>
              <UserName />
              <Password />
        </TransmissionHeader>
      <TransmissionBody>
      <!----- here goes GLogXMLElement XML element ---->
      </TransmissionBody>
   </Transmission>

I want to merge document 1) in to document 2) under <TransmissionBody> element.

Is it possible?

When I try to do that I get org.jdom.IllegalAddException: The Content already has an existing parent

SAXBuilder saxBuilder = new SAXBuilder();
        java.io.File xmlFile = new File("xmlDocA");
        org.jdom.Document xmlDocumentA = saxBuilder.build(xmlFile);
 
        xmlFile = new File("xmlDocB");
        org.jdom.Document xmlDocumentB = saxBuilder.build(xmlFile);
 
        Element rootA = xmlDocumentA.getRootElement();
        List childrenDocA = rootA.getChildren();
        
        Element rootB = xmlDocumentB.getRootElement();
        Element childDocB = rootB.getChild("TransmissionBody");
       
        childDocB.addContent(childrenDocA);                             ----------------- Exception at this line
 
        XMLOutputter xmlOutPutter = new XMLOutputter(Format.getPrettyFormat());
        xmlOutPutter.output(xmlDocumentB, System.out);

Open in new window

0
Comment
Question by:MehtaJasmin
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20292013
Try iterating the List and add the content yourself - ONLY after calling clone() on each Element
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20293056
>         List childrenDocA = rootA.getChildren();

change that to:

        List childrenDocA = xmlDocumentA.cloneContent();
0
 

Author Comment

by:MehtaJasmin
ID: 20323230
The alternate solution is:

replace

childDocB.addContent(childrenDocA);                            

to

childDocB.addContent(rootA.detach());                            
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question