Sample1:
1:新建XML文檔 books.xml,放到項目的根目錄下。
- <?xml version="1.0" encoding="GB2312"?>
- <books count="3"
- xmlns="http://test.org/books">
- <!--books's comment-->
- <book id="1">
- <name>Thinking in JAVA</name>
- </book>
- <book id="2">
- <name>Core JAVA2</name>
- </book>
- <book id="3">
- <name>C++ primer</name>
- </book>
- </books>
- package com.dom.test;
- import java.io.File;
- import java.io.IOException;
- 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.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class Test {
- public static void main(String[] args) throws ParserConfigurationException,
- SAXException, IOException {
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- /*
- * builder.parse()方法將給定文件的內容解析爲一個 XML 文檔, 並且返回一個新的 DOM Document對象。
- */
- Document document = builder.parse(new File("books.xml"));
- //打印document節點
- printNode(document,0);
- //獲取文檔的根元素,賦值給rootElement變量
- Element rootElement = document.getDocumentElement();
- //獲取根元素的count屬性
- int countOfBooks = Integer.parseInt(rootElement.getAttribute("count"));
- String str = rootElement.getAttribute("xmlns");
- System.out.println(str);
- System.out.println("There are "+countOfBooks+" books , they are ");
- //獲取rootElement的所有子節點(不包括屬性節點),返回一個NodeList對象
- NodeList childNodes = rootElement.getChildNodes();
- for(int i = 0;i < childNodes.getLength();i++){
- //獲取childNodes的第i個節點
- Node childNode = childNodes.item(i);
- //判斷childNode是不是一個元素節點,並且它的 nodeName 值爲book
- if(childNode.getNodeType() == Node.ELEMENT_NODE
- && childNode.getNodeName().equals("book")){
- //若是,則獲取childNode的所有子節點(不包括屬性節點),返回一個NodeList對象
- NodeList childNodes_2 = childNode.getChildNodes();
- for(int j = 0;j < childNodes_2.getLength();j++){
- //獲取childNodes_2的第j個節點
- Node childNode_2 = childNodes_2.item(j);
- //判斷childNode_2是不是一個元素節點,並且它的 nodeName 值爲name
- if(childNode_2.getNodeType() == Node.ELEMENT_NODE
- && childNode_2.getNodeName().equals("name")){
- //若是,則獲取childNode_2的所有子節點(不包括屬性節點),返回一個NodeList對象
- NodeList childNodes_3 = childNode_2.getChildNodes();
- for(int k = 0;k < childNodes_3.getLength();k++){
- //獲取childNodes_3的第k個節點
- Node childNode_3 = childNodes_3.item(k);
- //判斷childNodes_3是不是一個文本節點
- if(childNode_3.getNodeType() == Node.TEXT_NODE){
- //若是,則打印輸出這個文本節點的nodeValue
- System.out.println(" <<"+childNode_3.getNodeValue()+">>");
- }
- }
- }
- }
- }
- }
- }
- /*
- * 打印 DOM 節點
- * 輸出格式爲:
- * nodeType(nodeName,nodeValue)
- * "ATTRIBUTE"(attributeName=attributeValue)
- * ...
- * childNodeType[childNodeName,childNodeValue]
- * ...
- */
- public static void printNode(Node node,int count) {
- if (node != null) {
- String tmp = "";
- for(int i = 0 ; i < count ; i++) tmp += " ";
- //獲取node節點的節點類型,賦值給nodeType變量
- int nodeType = node.getNodeType();
- switch (nodeType) {
- case Node.ATTRIBUTE_NODE: tmp += "ATTRIBUTE";break;
- case Node.CDATA_SECTION_NODE: tmp += "CDATA_SECTION";break;
- case Node.COMMENT_NODE:tmp += "COMMENT";break;
- case Node.DOCUMENT_FRAGMENT_NODE:tmp += "DOCUMENT_FRAGMENT";break;
- case Node.DOCUMENT_NODE:tmp += "DOCUMENT";break;
- case Node.DOCUMENT_TYPE_NODE:tmp += "DOCUMENT_TYPE";break;
- case Node.ELEMENT_NODE:tmp += "ELEMENT";break;
- case Node.ENTITY_NODE:tmp += "ENTITY";break;
- case Node.ENTITY_REFERENCE_NODE:tmp += "ENTITY_REFERENCE";break;
- case Node.NOTATION_NODE:tmp += "NOTATION";break;
- case Node.PROCESSING_INSTRUCTION_NODE:tmp += "PROCESSING_INSTRUCTION";break;
- case Node.TEXT_NODE:tmp += "TEXT";break;
- default:return;//invalid node type.
- }
- System.out.println(tmp+" ("+node.getNodeName()+","+node.getNodeValue()+")");
- /*
- * node.getAttributes()方法返回
- * 包含node節點的屬性的 NamedNodeMap(如果它是 Element)
- */
- NamedNodeMap attrs = node.getAttributes();
- if(attrs != null)
- for(int i = 0 ; i < attrs.getLength() ; i++){
- printNode(attrs.item(i),count+1);
- }
- /*
- * node.getChildNodes()方法返回
- * 包含node節點的所有子節點的 NodeList。
- */
- NodeList childNodes = node.getChildNodes();
- for(int i = 0 ; i < childNodes.getLength() ; i++){
- printNode(childNodes.item(i),count+1);
- }
- }
- }
- }
Sample2:
1:新建XML文檔 persons.xml,放到項目的根目錄下。
- <?xml version="1.0" encoding="UTF-8"?>
- <persons>
- <person><name>kalision</name><sex>男</sex></person>
- <person>
- <name>kity</name>
- <sex>女</sex>
- </person>
- <person>
- <name>qizai</name>
- <sex>女</sex>
- </person>
- </persons>
- package net.vicp.jiasoft;
- import javax.xml.parsers.*;
- import java.io.IOException;
- import org.xml.sax.SAXException;
- import org.w3c.dom.Document;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.Node;
- public class DomXml {
- public void parsersXml() {
- //實例化一個文檔構建器工廠
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- try {
- //通過文檔構建器工廠獲取一個文檔構建器
- DocumentBuilder db = dbf.newDocumentBuilder();
- //通過文檔通過文檔構建器構建一個文檔實例
- Document doc = db.parse("persons.xml");
- //獲取所有名字爲 “person” 的節點
- NodeList nl1 = doc.getElementsByTagName("person");
- int size1 = nl1.getLength();
- System.out.println("該名稱的節點長度爲:" + size1);
- for (int i = 0; i < size1; i++) {
- Node n = nl1.item(i);
- //獲取 n 節點下所有的子節點。此處值得注意,在DOM解析時會將所有回車都視爲 n 節點的子節點。
- NodeList nl2 = n.getChildNodes();
- //因爲上面的原因,在此例中第一個 n 節點有 2 個子節點,而第二個 n 節點則有 5 個子節點(因爲多了3個回車)。
- int size2 = nl2.getLength();
- System.out.println("N節點的第一個子節點的長度爲:" + size2);
- for (int j = 0; j < size2; j++) {
- Node n2 = nl2.item(j);
- //還是因爲上面的原因,故此要處判斷當 n2 節點有子節點的時才輸出。
- if (n2.hasChildNodes()) {
- System.out.println(n2.getNodeName() + " = " +
- n2.getFirstChild().getNodeValue());
- }
- }
- }
- } catch (ParserConfigurationException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (SAXException ex) {
- ex.printStackTrace();
- }
- }
- public static void main(String[] args) {
- DomXml domxml = new DomXml();
- domxml.parsersXml();
- }
- }