XmlDocument.ImportNode() Example

完成的功能是从不同的Document复制Node到另一个Document的Node下

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>








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值