📄 ch12_4_3.java
字号:
/* 程序范例: Ch12_4_3.java */
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
public class Ch12_4_3 {
// 声明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);
// 新增子元素code
Element temp = (Element) document.createElement("code");
root.appendChild(temp);
temp.appendChild(document.createTextNode("F8915"));
// 新增子元素title
temp = (Element) document.createElement("title");
root.appendChild(temp);
temp.appendChild(document.createTextNode("ASP网页制作彻底研究"));
// 新增author元素
Element newNode = (Element) document.createElement("author");
root.insertBefore(newNode, root.getFirstChild());
Node newText = document.createTextNode("陈会安");
root.getFirstChild().appendChild(newText);
// 新增属性
temp = (Element) root.getFirstChild().getNextSibling();
temp.setAttribute("sales","Y");
// 显示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 + -