Solved

How do I merge two different XML document using JDOM

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now