Solved

How do I merge two different XML document using JDOM

Posted on 2007-11-15
3
1,377 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 125 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Setup GlassFish 4 34
program arguments vs VM arguments 4 25
American Express @Work site and Java 4 63
Github api to fetch the number of collaborators in a git repo 1 25
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

679 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