Solved

How do I merge two different XML document using JDOM

Posted on 2007-11-15
3
1,399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to count occurrences of each item in an array.
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

632 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