[jdom-interest] JDOM Clone Help

Perturbed dravisiddhu at gmail.com
Tue Jul 25 13:11:13 PDT 2006


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>something</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").addContent((Element)(doc.getRootElement().getChild("ModuleResponse").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
-- 
View this message in context: http://www.nabble.com/JDOM-Clone-Help-tf2000298.html#a5492362
Sent from the JDOM - General forum at Nabble.com.



More information about the jdom-interest mailing list