📄 add_delxml.java
字号:
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
public class Add_DelXML
{
// 声明XML文件
static Document document;
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 设定解析的叁数
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
// 建立新XML文件
document = db.newDocument();
// 建立根元素
Element root = (Element) document.createElement("book");
document.appendChild(root);
// 新增子元素price
Element temp = (Element) document.createElement("price");
root.appendChild(temp);
temp.appendChild(document.createTextNode("38"));
// 新增子元素title
temp = (Element) document.createElement("title");
root.appendChild(temp);
temp.appendChild(document.createTextNode("JSP动态网页设计"));
// 新增author元素
Element newNode = (Element) document.createElement("author");
root.insertBefore(newNode, root.getFirstChild());
Node newText = document.createTextNode("王五");
root.getFirstChild().appendChild(newText);
// 新增属性
temp = (Element) root.getElementsByTagName("price").item(0);
temp.setAttribute("sale","Y");
System.out.println("建立的XML文件: ");
printXML(root);
// 删除author元素
root.removeChild((Element) root.getElementsByTagName("author").item(0));
// 删除price属性sale
Element delNode=(Element) root.getElementsByTagName("price").item(0);
delNode.removeAttribute("sale");
System.out.println("\n删除后的XML文件: ");
printXML(root);
}
private static void printXML(Node root)
{
// 显示XML文件
System.out.println("根元素: " + root.getNodeName());
NodeList nodes = root.getChildNodes();
// 获取所有的子节点
for (int i=0; i < nodes.getLength(); i++)
{
// 元素和文字节点
System.out.print("元素: " + nodes.item(i).getNodeName());
System.out.println("/" + nodes.item(i).getFirstChild().getNodeValue());
// 显示指定元素的属性值
if (nodes.item(i).hasAttributes()) {
NamedNodeMap atts = nodes.item(i).getAttributes();
for (int j = 0; j < atts.getLength(); j++)
{
Node att = atts.item(j);
System.out.print(" +-- " + att.getNodeName());
System.out.println("/" + att.getNodeValue());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -