Xerces 简明教程

DOM Parser - Parse XML Document

Steps to Use DOM

以下是使用 DOM 解析器解析文档时使用的步骤。

  1. Import XML-related packages.

  2. Create a DocumentBuilder

  3. 从文件或流创建一个文档

  4. Extract the root element

  5. Examine attributes

  6. Examine sub-elements

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;

Create a DocumentBuilder

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Create a Document from a file or stream

StringBuilder xmlStringBuilder = new StringBuilder();
xmlStringBuilder.append("<?xml version = "1.0"?> <class> </class>");
ByteArrayInputStream input =  new ByteArrayInputStream(
   xmlStringBuilder.toString().getBytes("UTF-8"));
Document doc = builder.parse(input);

Extract the root element

Element root = document.getDocumentElement();

Examine attributes

//returns specific attribute
getAttribute("attributeName");
//returns a Map (table) of names/values
getAttributes();

Examine sub-elements

//returns a list of subelements of specified name
getElementsByTagName("subelementName");
//returns a list of all child nodes
getChildNodes();

Demo Example

下面是需要解析的输入 xml 文件 −

<?xml version = "1.0"?>
<class>

   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>
   </student>

   <student rollno = "493">
      <firstname>Vineet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>

   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

Demo Example

DomParserDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class DomParserDemo {
   public static void main(String[] args){

      try {
         File inputFile = new File("input.txt");
         DocumentBuilderFactory dbFactory
            = DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         Document doc = dBuilder.parse(inputFile);
         doc.getDocumentElement().normalize();

         System.out.println("Root element :"
            + doc.getDocumentElement().getNodeName());
         NodeList nList = doc.getElementsByTagName("student");

         System.out.println("----------------------------");
         for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :"
               + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;

               System.out.println("Student roll no : "
                  + eElement.getAttribute("rollno"));

               System.out.println("First Name : "
                  + eElement
                  .getElementsByTagName("firstname")
                  .item(0)
                  .getTextContent());

               System.out.println("Last Name : "
               + eElement
                  .getElementsByTagName("lastname")
                  .item(0)
                  .getTextContent());

               System.out.println("Nick Name : "
               + eElement
                  .getElementsByTagName("nickname")
                  .item(0)
                  .getTextContent());

               System.out.println("Marks : "
               + eElement
                  .getElementsByTagName("marks")
                  .item(0)
                  .getTextContent());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

以上程序将生成以下结果:

Root element :class
----------------------------

Current Element :student
Student roll no : 393
First Name : Dinkar
Last Name : Kad
Nick Name : Dinkar
Marks : 85

Current Element :student
Student roll no : 493
First Name : Vineet
Last Name : Gupta
Nick Name : Vinni
Marks : 95

Current Element :student
Student roll no : 593
First Name : Jasvir
Last Name : Singh
Nick Name : Jazz
Marks : 90