⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e539. adding a node to a dom document.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
Home > List of Packages > org.w3c.dom  [30 examples] > Adding and Removing Nodes  [6 examples] 

e539. Adding a Node to a DOM Document
This example demonstrates how to insert a node into a DOM relative to another node. In particular, a text node is inserted around an element node. 
    // Create a new DOM document; this method is implemented in
    // e511 Creating an Empty DOM Document
    Document doc = createDomDocument();
    
    // Insert the root element node
    Element element = doc.createElement("root");
    doc.appendChild(element);
    
    // Insert a comment in front of the element node
    Comment comment = doc.createComment("a comment");
    doc.insertBefore(comment, element);
    
    // Add a text node to the element
    element.appendChild(doc.createTextNode("D"));
    
    // Add a text node to the beginning of the element
    element.insertBefore(doc.createTextNode("A"), element.getFirstChild());
    
    // Add a text node before the last child of the element
    element.insertBefore(doc.createTextNode("C"), element.getLastChild());
    
    // Add another element after the first child of the root element
    Element element2 = doc.createElement("item");
    element.insertBefore(element2, element.getFirstChild().getNextSibling());
    
    // Add a text node in front of the new item element
    element2.getParentNode().insertBefore(doc.createTextNode("B"), element2);
    

This is the resulting XML: 
    <?xml version="1.0" encoding="UTF-8"?>
    <!--a comment--><root>AB<item/>CD</root>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -