Source Code : Java DOM edit: Copy a Node from One Parse Tree into Another

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Java DOM edit: Copy a Node from One Parse Tree into Another

      

//An XML Document Providing an Element for Export
/*
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE contacts [
<!ELEMENT contacts (person)*>
<!ELEMENT person (name, email)>
<!ELEMENT name (#PCDATA | bold)*>
<!ELEMENT email (#PCDATA)>
]>

<contacts>
    <person>
        <name>Ichobad Crane</name>
        <email>sleepy@hollow.net</email>
    </person>
</contacts>*/

//An XML Document to Receive an Imported Element
/*
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE folks [
<!ELEMENT folks (person)*>
<!ELEMENT person (name, phone, email)>
<!ELEMENT name (#PCDATA | bold)*>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT email (#PCDATA)>
]>

<folks>
    <person>
        <name>Zaphod Beeblebrox</name>
        <phone>907 555-9882</phone>
        <email>outer@space.net</email>
    </person>
</folks>
*/


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

public class DOMImport {
    static public void main(String[] arg) {
        if(arg.length != 3) {
            System.err.println(
                    "Usage: DOMImport <infile1> <infile2> <outfile>");
            System.exit(1);
        }
        DOMImport dc = new DOMImport();
        dc.inandout(arg[0],arg[1],arg[2]);
    }
    public void inandout(String infile1,String infile2,String outfile) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc1 = null;
        Document doc2 = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is1 = new InputSource(infile1);
            doc1 = builder.parse(is1);
            InputSource is2 = new InputSource(infile2);
            doc2 = builder.parse(is2);
            importName(doc1,doc2);
            FileOutputStream fos = new FileOutputStream(outfile);
            TreeToXML ttxml = new TreeToXML();
            ttxml.write(fos,doc2);
            fos.close();
        } catch (SAXException e) {
            System.exit(1);
        } catch (ParserConfigurationException e) {
            System.err.println(e);
            System.exit(1);
        } catch (IOException e) {
            System.err.println(e);
            System.exit(1);
        }
    }
    public void importName(Document doc1,Document doc2) {
        Element root1 = doc1.getDocumentElement();
        Element personInDoc1 = (Element)root1.getFirstChild();

        Node importedPerson = doc2.importNode(personInDoc1,true);

        Element root2 = doc2.getDocumentElement();
        root2.appendChild(importedPerson);
    }
}


           
         
    
    
    
    
    
  

Thank with us