Solved

How to build multiple XML documents by mergeing two XML documents using JDOM.

Posted on 2007-11-23
3
212 Views
Last Modified: 2013-11-18
I am building multiple dynamic xml documents from result set and storing them to an ArrayList called 'xmlDocumentList'.
I have another static xml document called 'staticDocument'. Where static document looks like:

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

Now I want to create another ArrayList 'finalDocumentList' out of mergeing 'staticDocument' with each element of 'xmlDocumentList'. That will be like each element of 'finalDocumentList' is made from merging 'staticDocument' with 'xmlDocumentList[i]' (one of dynamic document element) at <TransmissionBody>. When I do that with attached code snippet, it creates the GlogFinal0.xml, GlogFinal1, GlogFinal2..etc file with all having same content in them. It uses only first element of 'xmlDocumentList' to build all elements of 'finalDocumentList'.



ArrayList dynamicDocumentList = xmlBuilder.getDynamicXmlDocuments();

        Document staticDocument = xmlBuilder.getStaticXmlWrapper();

        ArrayList finalDocumentList = new ArrayList(); 

        Element staticRoot = staticDocument.getRootElement();

        Element staticChild = staticRoot.getChild("TransmissionBody");

        Element dynamicRoot = null;

        for(int i = 0; i < dynamicDocumentList.size(); i++)

        {

            dynamicRoot = ((Document)dynamicDocumentList.get(i)).getRootElement();

            staticChild.addContent(dynamicRoot.detach()); 

           finalDocumentList.add(staticDocument);

        }

          FileOutputStream finalXmlFile = null;

          XMLOutputter xmlOutPutter = null;

          for (int i = 0; i < finalDocumentList.size(); i++)

          {

            finalXmlFile =  new FileOutputStream("F:\\JavaProjects\\WorkspaceDev\\OTM\\xsl\\GLogFinal"+i+".xml");

            xmlOutPutter = new XMLOutputter(Format.getPrettyFormat());

            xmlOutPutter.output((Document)(finalDocumentList.get(i)), finalXmlFile);

          }

Open in new window

0
Comment
Question by:MehtaJasmin
  • 2
3 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 20339365
Its copying the whole thing because you are attaching the root nodes of a Document.

I think that xmlBuilder.getDynamicXmlDocuments()  is incorrect. Perhaps the method should create new documents each containing the node of interest. The rest of the code looks correct.
0
 

Author Comment

by:MehtaJasmin
ID: 20339947
I know that xmlBuilder.getDynamicXmlDocuments()  is creating correct ArrayList out of the result set. I have printed the list before. It shows correct results.

 Attached is the code for that method.

The problem is merging of static doc with dynamic element in loop.
    private ArrayList getDynamicXmlDocuments()

    {

        Statement stmt = null;

        ResultSet rs = null;

        org.w3c.dom.Document document = null;

        oracle.xml.parser.v2.XMLDocument xmlDocument = null;

        org.jdom.input.DOMBuilder domBuilder = null;

        ArrayList xmlDocumentList = new ArrayList();

        Document jdomDocument = null;

        XMLType xmlType = null;

        String sql = "select OTM from OTM_XML";
 

        try

        {

            stmt = 

(ConnectionManager.getInstance()).getConnection().createStatement();

            logger.info("SQL: " + sql);

            rs = stmt.executeQuery(sql);

            OracleResultSet orset = (OracleResultSet)rs;

            while (orset.next())

            {   

                xmlType = XMLType.createXML(orset.getOPAQUE(1));

                document = xmlType.getDOM();

                xmlDocument = (oracle.xml.parser.v2.XMLDocument)document;
 

                domBuilder = new org.jdom.input.DOMBuilder();

                jdomDocument = domBuilder.build(document);

                xmlDocumentList.add(jdomDocument);

            }

            System.out.println("xml document list size : "+xmlDocumentList.size());

            FileOutputStream out = null;

            XMLOutputter xmlOutPutter = null;

            for (int i = 0; i < xmlDocumentList.size(); i++)

            {

              out =  new FileOutputStream("F:\\JavaProjects\\WorkspaceDev\\OTM\\xsl\\GLogDynamic"+i+".xml");

              xmlOutPutter = new XMLOutputter(Format.getPrettyFormat());

              xmlOutPutter.output((Document)(xmlDocumentList.get(i)), out);

            }    

        }

        catch (SQLException se)

        {

            logger.error(RootException.getStackTraceString(se));

        }

        catch (Exception se)

        {

            logger.error(RootException.getStackTraceString(se));

        }

        finally

        {

            try

            {

                if (stmt != null)

                {

                    stmt.close();

                    stmt = null;

                }

            }

            catch (SQLException se)

            {

                System.out.println("SQLException in finally of statement: " + 

                                   se.getMessage());

                logger.info("SQLException in finally of statement: " + 

                            se.getMessage());

            }

            try

            {

                if (rs != null)

                {

                    rs.close();

                    rs = null;

                }

            }

            catch (SQLException se)

            {

                System.out.println("SQLException in finally of resultset: " + 

                                   se.getMessage());

                logger.info("SQLException in finally of resultset: " + 

                            se.getMessage());

            }

        }
 

        return xmlDocumentList;

    }

Open in new window

0
 
LVL 13

Accepted Solution

by:
kawas earned 125 total points
ID: 20340038
Hmm, they are all the same, because in your for loop, you are adding the same element every time

staticChild.addContent(dynamicRoot.detach());

All of the elements are 'live' (changes occur even after you add them to other documents) so for each iteration of the for loop staticChild keeps adding more children and all the nodes in the list finalDocumentList are getting modified.

Does this make sense?

I think that the best solution would be to create new elements in the for loop so that they have limited scope and you cant modify them inadvertently.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 39
micro services vs rest web services 16 85
Cordova Camera plugin fails 2 96
nextBoolean(double p) for Random class 3 35
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…
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.
This video teaches viewers about errors in exception handling.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

22 Experts available now in Live!

Get 1:1 Help Now