Here is the code... Enjoy!
----------------------------START--------------------------------
package java_practice.saxpractice;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.*;
// For write operation
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class TransformationApp02
{
// Global values
static Document documentA;
static Document documentB;
public static void main (
String argv [])
{
//Directory path where the files are located.
String directorypath = argv[0];
String aXml = directorypath+"A.xml";
String bXml = directorypath+"B.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
File fA = new File(aXml);
File fB = new File(bXml);
DocumentBuilder builder = factory.newDocumentBuilder();
documentA = builder.parse(fA);
documentB = builder.parse(fB);
NodeList nodeListA = documentA.getElementsByTagName("B");
for(int count=0 ; count<nodeListA.getLength();count++)
{
Node nodeA = nodeListA.item(count);
Node temp = documentB.importNode(nodeA,true);
Element element = documentB.getDocumentElement();
element.appendChild(temp);
}
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(documentB);
StreamResult result = new StreamResult(bXml);
transformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println ("\n** Transformer Factory error");
System.out.println(" " + tce.getMessage() );
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();
} catch (TransformerException te) {
// Error generated by the parser
System.out.println ("\n** Transformation error");
System.out.println(" " + te.getMessage() );
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
}
}
-----------------------------END---------------------------------
[ March 17, 2006: Message edited by: Amy Medrat ]