?? ch12_3_3.java
字號:
/* 程序范例: Ch12_3_3.java */
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
public class Ch12_3_3 {
// 聲明XML文件
static Document document;
public static void main(String[] args) throws SAXException {
if (args.length != 1) {
System.out.println("使用: java Ch12_3_3 xml_file");
return;
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
dbf.setValidating(true);
// 設定解析的叁數
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(false);
dbf.setCoalescing(false);
DocumentBuilder db = dbf.newDocumentBuilder();
// 指定錯誤處理物件
db.setErrorHandler(new parserErrorHandler());
// 導入XML文件
document = db.parse(new File(args[0]));
// 取得文件的子節點
Node child = (Node)document.getFirstChild();
// 取得這一層的所有節點
for ( ; child != null; child = child.getNextSibling()) {
System.out.print("元素: " + child.getNodeName());
System.out.println("/類型: " + child.getNodeType());
printChildNode(child, 0);
}
} catch(ParserConfigurationException pe) {
// 解析器設定錯誤
pe.printStackTrace();
} catch(IOException ie) {
// 文件處理錯誤
ie.printStackTrace();
}
}
// 解析的錯誤處理物件
private static class parserErrorHandler implements ErrorHandler {
// 獲取錯誤信息
private String getMsg(SAXParseException spe) {
String sysId = spe.getSystemId();
if (sysId == null) {
sysId = "NULL";
}
String msg = "URI=" + sysId + " 行號=" + spe.getLineNumber() +
": " + spe.getMessage();
return msg;
}
// 標準SAX ErrorHandler方法
public void warning(SAXParseException spe) throws SAXException {
System.out.println("\n警告: " + getMsg(spe));
}
public void error(SAXParseException spe) throws SAXException {
String message = "\n錯誤: " + getMsg(spe);
throw new SAXException(message);
}
public void fatalError(SAXParseException spe) throws SAXException {
String message = "\n嚴重錯誤: " + getMsg(spe);
throw new SAXException(message);
}
}
// 顯示子節點的返回方法
private static void printChildNode(Node temp, int pos) {
if (temp.hasChildNodes()) {
NodeList nodes = temp.getChildNodes();
// 獲取所有的子節點
for (int i=0; i < nodes.getLength(); i++) {
int type = nodes.item(i).getNodeType();
if (type == Node.ELEMENT_NODE) {
printIndent(pos);
System.out.println(" 元素(" + i + "):" + nodes.item(i).getNodeName());
printChildNode(nodes.item(i), pos+1);
}
if (type == Node.TEXT_NODE) {
printIndent(pos);
System.out.print(" 元素(" + i + "): " + nodes.item(i).getNodeName());
String val = nodes.item(i).getNodeValue();
if (val != null) {
System.out.print("/元素值: ");
if (val.trim().equals(""))
System.out.println("[WS]");
else
System.out.println(val);
}
}
}
}
}
// 顯示縮排的字符
private static void printIndent(int num) {
System.out.print(" +");
for (int i = 0; i <= num; i++) {
System.out.print("--");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -