Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2006, 03:48 PM
Registered User
 
Join Date: Jul 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default JDOM Combining two XML documents Clone help

I need help with this problem.

I have two example XML documents

XML 1

<?xml version="1.0" encoding="UTF-8"?>
<Service_Request_1.0 type="VALIDATION" job_identifier="01010">
        <Groups>
        </Groups>
</Service_Request_1.0>

XML 2

<?xml version="1.0" encoding="UTF-8"?>
<RemoveResponse>
        <ModuleResponse>
                <message>something</message>
        </ModuleResponse>
        <ModuleResponse>
                <message>some other thing</message>
        </ModuleResponse>
</RemoveResponse>



My task is to put XML2 <ModuleResponse> children into the <Groups> of the XML 1.

This is the code that I am trying to use:

import org.xml.sax.InputSource;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import org.jdom.*;
import java.io.*;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JDOMExample {
  public static void main(String[] args)
  {

    SAXBuilder builder = new SAXBuilder();
    String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Service_Request_1.0 type=\"VALIDATION\" job_identifier=\"01010\"><Groups></Groups></Service_Request_1.0>";
    String strs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><RemoveResponse><ModuleResponse><message>some thing</message></ModuleResponse><ModuleResponse><message>some other thing</message></ModuleResponse></RemoveResponse>";
    try {

      Document maindoc = builder.build(new InputSource(new StringReader(str)));
      Document doc = builder.build(new InputSource(new StringReader(strs)));
      Element rootElement = doc.getRootElement();
                        maindoc.getRootElement().getChild("Groups").addCon tent((Element)(doc.getRootElement().getChild("Modu leResponse").clone()));
    Format format = Format.getPrettyFormat();
                        XMLOutputter serializer = new XMLOutputter(format);
                        String servicereply = serializer.outputString(maindoc);
                        System.out.println("Response:"+servicereply);
    }

    catch (JDOMException e) {
      System.out.println("Document is not well-formed.");
      System.out.println(e.getMessage());
    }
    catch (IOException e) {
      System.out.println("Could not check Document");
      System.out.println(" because " + e.getMessage());
    }

  }

}



When I try to do the clone I only get the first <ModuleResponse>. Is there a way for me to copy all children so that I will have XML like this

<?xml version="1.0" encoding="UTF-8"?>
<Service_Request_1.0 type="VALIDATION" job_identifier="01010">
        <Groups>

        <ModuleResponse>
                <message>something</message>
        </ModuleResponse>
        <ModuleResponse>
                <message>some other thing</message>
        </ModuleResponse>

        </Groups>
</Service_Request_1.0>



Please help me on this issue. Thanks in anticipation

 
Old July 25th, 2006, 05:46 PM
Registered User
 
Join Date: Jul 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never Mind :-) .. Got it working. Used getChildren().

 
Old July 25th, 2006, 06:01 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's a list specially for JDOM at www.jdom.org, I suggest you ask there.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining string paths elements into another XML eXhumed XSLT 2 March 19th, 2008 11:06 AM
compare two xml documents bcogney XSLT 1 October 30th, 2006 03:33 PM
Basic doubt in combining XML Documents rajatake XSLT 2 May 4th, 2006 04:30 AM
beginners problems (combining xml feeds etc) bewise XSLT 0 February 7th, 2006 06:39 AM
merging 2 xml documents espider4u XSLT 0 August 31st, 2004 11:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.