Home Tutorial Xml Dom DOM importnode, Example of importnode method of document interface.

 
 

DOM importnode, Example of importnode method of document interface.
Posted on: September 23, 2010 at 12:00 AM
Advertisement
DOM importnode, Example of importnode method of document interface.

XmlDocument.ImportNode() Example

In this example, we are going to know how we can make a program to import a node from another document to the current document.

The importNode () method creates the duplicate of the node in the document from another document, including its data. The sub nodes of the source node are recursively imported and the resulting nodes make the corresponding sub tree. The default attributes are not copied from the source document .

The method importNode(Node importedNode, boolean deep) have two parameters,

Node- the node which is to be imported.

deep-  if true , performing deep clone on document,if false -copy only the imported node only not data or subnode.

The method importNode(Node importedNode, boolean deep) does not import the attribute values even on a deep clone. We try to import an element from hr.xml into orders.xml under the root.

hr.xml.

<?xml version="1.0" encoding="UTF-8"?>
                 
<hr>
     <person>
        <first>Kiran</first>
        <last>Pal</last>
        <age>22</age>
      </person>
 </hr>

orders.xml.

<?xml version="1.0" encoding="UTF-8"?>
                    
<orders>
         <item>
           <name>blanket</name>
           <price>20</price>
           <qty>10</qty>
       </item>
 </orders>

importNodeExample.java.

import org.w3c.dom.*;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

class importNodeExample 
{
  public static void main(String[] args
  {
    try{
   
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document docA = docBuilder.parse("hr.xml");
    Document docB = docBuilder.parse("orders.xml");
    Element rootA = docA.getDocumentElement();
    Element rootB = docB.getDocumentElement();
      rootB.appendChild(docB.importNode(rootA,true));
    TransformerFactory tranFactory = TransformerFactory.newInstance()
    Transformer aTransformer = tranFactory.newTransformer()
    Source src = new DOMSource(docB)
    Result dest = new StreamResult(System.out)
    aTransformer.transform(src, dest)

        }catch(Exception e){
           System.out.println(e.getMessage());
         }
     }
}

Output of the program.

C:\>javac importNodeExample.java

C:\>java importNodeExample
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<orders>
      <item>
          <name>blanket</name>
          <price>20</price>
          <qty>10</qty>
     </item>
    < hr>
        <person>
           <first>Kiran</first>
           <last>Pal</last>
           <age>22</age>
       </person>
    </hr>

</orders>

Download The Example.

Advertisement

Related Tags for DOM importnode, Example of importnode method of document interface.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com