2008年4月21日 Edited By DingDangXiaoMa
DOM 方法對XML的解析,是讀取整個.xml文件,把信息存儲到內存中形成樹型結構,然后再對各結點進行處理。
好處:簡單,方便。
壞處:由于讀取整個文件再進行處理,處理大文件時,占用大量內存資料。
example:
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class MyDOMParser {
// 名字空間
private String strNamespace = "http://www.lit.edu.cn/student/";
// 一個學生的資料
private Hashtable htbStudent = new Hashtable();
// 所有學生的向量列表
private Vector vStuInfo = new Vector();
public MyDOMParser() {
}
public static void main(String[] args) {
MyDOMParser myDOMParser = new MyDOMParser();
myDOMParser.parseXMLFile("http://localhost/example/xml/SutInfo.xml");
}
/**
* 解析文檔
* @param fileURI
*/
public void parseXMLFile(String fileURI) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 允許名字空間
factory.setNamespaceAware(true);
// 允許驗證
factory.setValidating(true);
// 獲得DocumentBuilder的一個實例
DocumentBuilder builder = factory.newDocumentBuilder();
// 解析文檔,并獲得一個Document實例。
Document doc = builder.parse(fileURI);
// 獲得根節點StuInfo
Element elmtStuInfo = doc.getDocumentElement();
// 得到所有student節點
NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(strNamespace, "student");
System.out.println("**** Student information start ****");
// 循環輸出每一個學生資料
for (int i = 0; i < nlStudent.getLength(); i++) {
// 當前student節點元素
Element elmtStudent = (Element) nlStudent.item(i);
NodeList nlCurrent = elmtStudent.getElementsByTagNameNS(strNamespace, "name");
System.out.println("Name:"+ nlCurrent.item(0).getFirstChild().getNodeValue());
nlCurrent = elmtStudent.getElementsByTagNameNS(strNamespace,"sex");
System.out.println("Sex:"+ nlCurrent.item(0).getFirstChild().getNodeValue());
nlCurrent = elmtStudent.getElementsByTagNameNS(strNamespace,"lesson");
for (int j = 0; j < nlCurrent.getLength(); j++) {
Element elmtLesson = (Element) nlCurrent.item(j);
NodeList nlLesson = elmtLesson.getElementsByTagNameNS(strNamespace, "lessonName");
System.out.print(nlLesson.item(0).getFirstChild().getNodeValue());
System.out.print(":");
nlLesson = elmtLesson.getElementsByTagNameNS(strNamespace,"lessonScore");
System.out.print(nlLesson.item(0).getFirstChild().getNodeValue());
System.out.print("\n");
}
System.out.println("------------------------------------");
}
System.out.println("**** Student information end ****");
} catch (SAXException saxe) {
System.out.println("error1
");
System.out.println(saxe.getMessage());
} catch (IOException ioe) {
System.out.println("error2
");
System.out.println(ioe.getMessage());
} catch (ParserConfigurationException pce) {
System.out.println("error3
");
System.out.println(pce.getMessage());
}
}
}
輸出結果如下:
**** Student information start ****
Name:bigmouse
Sex:male
math:60
Englist:59
autoCAD:80
SCM:90
mechanics:61
------------------------------------
Name:coco
Sex:female
math:90
Englist:95
C++:80
Java:85
------------------------------------
**** Student information end ****
分析:
1.從上面導入的包就可以看出主要應用:javax.xml.parsers 和 org.w3c.dom 這兩個包都是基礎包,jdk 中有。因此DOM方式不用導入其它包就可運行。又可以看出得到了w3c組織的認可。其實這個DOM 方式還結合了.jaxp 也就是java api for xml parser
2.最主要的一點,是命名空間的使用,所有的結點也都在在命名空間中所定義的。
3。讀取xml產生Document 對象。NodeList 中遍歷所的結點。
這個例子就到這里,下一篇是JDOM方法。