Xerces 简明教程

Apache Xerces - Quick Guide

Apache Xerces - XML Overview

What is XML?

XML 是一种简单的基于文本的语言,旨在以纯文本格式存储和传输数据。它代表可扩展标记语言。以下是 XML 的一些显著功能。

XML is a simple text-based language which was designed to store and transport data in plain text format. It stands for Extensible Markup Language. Following are some of the salient features of XML.

  1. XML is a markup language.

  2. XML is a tag based language like HTML.

  3. XML tags are not predefined like HTML.

  4. You can define your own tags which is why it is called extensible language.

  5. XML tags are designed to be self-descriptive..

  6. XML is a W3C Recommendation for data storage and transport.

Example

<?xml version = "1.0"?>
<Class>
   <Name>First</Name>
   <Sections>

      <Section>
         <Name>A</Name>
         <Students>
            <Student>Rohan</Student>
            <Student>Mohan</Student>
            <Student>Sohan</Student>
            <Student>Lalit</Student>
            <Student>Vinay</Student>
         </Students>
      </Section>

      <Section>
         <Name>B</Name>
         <Students>
            <Student>Robert</Student>
            <Student>Julie</Student>
            <Student>Kalie</Student>
            <Student>Michael</Student>
         </Students>
      </Section>

   </Sections>
</Class>

Advantages

XML 提供的优点如下:

Following are the advantages provided by XML −

  1. Technology agnostic − Being plain text, XML is technology independent. It can be used by any technology for data storage and transmission purpose.

  2. Human readable − XML uses simple text format. It is human readable and understandable.

  3. Extensible − In XML, custom tags can be created and used very easily.

  4. Allow Validation − Using XSD, DTD and XML structure can be validated easily.

Disadvantages

以下是 XML 用法的缺点:

Following are the disadvantages of XML usage −

  1. Redundant Syntax − Normally XML file contains numerous repetitive terms.

  2. Verbose − Being a verbose language, XML file size increases the transmission and storage costs.

Apache Xerces - Environment Setup

本教程将带你完成在 Windows 和基于 Linux 的系统中设置 Apache Xerces 的过程。按照几个简单的步骤,在没有复杂设置程序的情况下,可以轻松安装 Apache Xerces 并将其集成到你的当前 Java 环境中。安装过程中需要用户管理。

This chapter takes you through the process of setting up Apache Xerces on Windows and Linux based systems. Apache Xerces can be easily installed and integrated with your current Java environment following a few simple steps without any complex setup procedures. User administration is required while installation.

System Requirements

JDK

Java SE 2 JDK 1.5 or above

Memory

1 GB RAM (recommended)

Disk Space

No minimum requirement

Operating System Version

Windows XP or above, Linux

现在,让我们继续进行安装 Apache Xerces 的步骤。

Let us now proceed with the steps to install Apache Xerces.

Step 1: Verify your Java Installation

首先,你所使用的系统需要安装 Java 软件开发套件 (SDK)。要验证这一点,请根据你所使用的平台执行两个命令中的任何一个。

First of all, you need to have the Java Software Development Kit (SDK) installed on your system. To verify this, execute any of the two commands depending on the platform you are working on.

如果 Java 安装成功,那么它将显示你的 Java 安装的当前版本和规范。一个示例输出在以下表格中给出。

If the Java installation successful, then it will display the current version and specification of your Java installation. A sample output is given in the following table.

Platform

Command

Sample Output

Windows

Open Command Console and type − java -version

Java version "1.7.0_60" Java ™ SE Run Time Environment (build 1.7.0_60-b19) Java Hotspot ™ 64-bit Server VM (build 24.60b09,mixed mode)

Linux

Open command terminal and type − $java -version

Java version "1.7.0_25" Open JDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64) Open JDK 64-Bit Server VM (build 23.7-b01, mixed mode)

  1. We assume the readers of this tutorial have Java SDK version 1.7.0_60 installed on their system.

  2. In case you do not have Java SDK, download its current version from www.oracle.com/technetwork/java/javase/downloads/index.html and have it installed.

Step 2: Set your Java Environment

设置环境变量 JAVA_HOME 以指向计算机上安装 Java 的基本目录位置。例如,

Set the environment variable JAVA_HOME to point to the base directory location where Java is installed on your machine. For example,

Sr.No.

Platform & Description

1

Windows Set JAVA_HOME to C:\ProgramFiles\java\jdk1.7.0_60

2

Linux Export JAVA_HOME = /usr/local/java-current

将 Java 编译器位置的完整路径附加到系统路径。

Append the full path of the Java compiler location to the System Path.

Sr.No.

Platform & Description

1

Windows Append the String "C:\Program Files\Java\jdk1.7.0_60\bin" to the end of the system variable PATH.

2

Linux Export PATH = $PATH:$JAVA_HOME/bin/

如上所示从命令提示符处执行命令 java version。

Execute the command java version from the command prompt as explained above.

Step 3: Install Apache Xerces Library

https://xerces.apache.org/mirrors.cgi" 下载 Apache Xerces 的最新版本,并将其内容解压到一个文件夹中,以便从该文件夹链接必需的库到你的 Java 程序中。假设这些文件已经收集到 C 驱动器上的一个文件夹 xerces-2_12_1 中。

Download the latest version of Apache Xerces from https://xerces.apache.org/mirrors.cgi" and unzip its contents to a folder from where the required libraries can be linked to your Java program. Let us assume the files are collected in a folder xerces-2_12_1 on the C drive.

将上图中突出显示的五个 jar 的完整路径添加到 CLASSPATH。

Add the complete path of the five jars as highlighted in the above image to the CLASSPATH.

Sr.No.

Platform & Description

1

Windows Append the following strings to the end of the user variable CLASSPATH − C:\xerces-2_12_1\resolver.jar; C:\xerces-2_12_1\serializer.jar; C:\xerces-2_12_1\xercesImpl.jar; C:\xerces-2_12_1\xercesSamples.jar; C:\xerces-2_12_1\xml-apis.jar;

2

Linux Export CLASSPATH = $CLASSPATH − /usr/share/xerces-2_12_1\resolver.jar; /usr/share/xerces-2_12_1\serializer.jar; /usr/share/xerces-2_12_1\xercesImpl.jar; /usr/share/xerces-2_12_1\xercesSamples.jar; /usr/share/xerces-2_12_1\xml-apis.jar;

Apache Xerces - XML Parsers

What is Apache Xerces2?

Xerces2 是基于 Java 的处理器,并提供以下 XML 解析 API 标准的标准接口和实现:

Xerces2 is a Java based processor and provides standard interfaces and implementations for following XML parsing API standards −

  1. Document Object Model (DOM) Level 3

  2. Simple API for XML (SAX) 2.0.2

  3. Streaming API for XML (StAX) 1.0 Event API

  4. Java APIs for XML Processing (JAXP) 1.4

What is XML Parsing?

解析 XML 指的是遍历 XML 文档来访问数据或通过一种或另一种方式修改数据。

Parsing XML refers to going through the XML document to access data or to modify data in one or the other way.

What is XML Parser?

XML 解析器提供了一种访问或修改 XML 文档中存在的数据的方法。Java 提供了多种用于解析 XML 文档的选项。以下是用于解析 XML 文档的各种类型的解析器。

XML Parser provides a way to access or modify the data present in an XML document. Java provides multiple options to parse XML document. Following are various types of parsers which are commonly used to parse XML documents.

  1. Dom Parser − Parses the document by loading the complete contents of the document and creating its complete hierarchical tree in memory.

  2. SAX Parser − Parses the document on event based triggers. Does not load the complete document into the memory.

  3. StAX Parser − Parses the document in similar fashion to SAX parser but in a more efficient way.

现在,我们将在后续章节中使用 Apache Xerces 库来详细说明每个解析器。

Now, we will elaborate each parser using the Apache Xerces library in our subsequent chapters.

Apache Xerces - DOM Parser Overview

文档对象模型是万维网联盟(W3C)的正式建议。它定义了使程序能够访问和更新 XML 文档的样式、结构和内容的接口。支持 DOM 的 XML 解析器实施了该接口。

The Document Object Model is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure, and contents of the XML documents. XML parsers that support the DOM, implement that interface.

When to use?

你应在下列情况下使用 DOM 解析器:

You should use a DOM parser when −

  1. You need to know a lot about the structure of a document.

  2. You need to move parts of the document around (you might want to sort certain elements, for example).

  3. You need to use the information in the document more than once.

What you get?

使用 DOM 解析器解析 XML 文档时,你会得到一个包含你的文档所有元素的树形结构。DOM 提供了各种函数让你能够检查文档的内容和结构。

When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.

Advantages

DOM 是用于操作文档结构的通用接口。它的一个设计目标是,为一个 DOM 兼容解析器编写的 Java 代码应该可以在任何其他 DOM 兼容解析器上运行,无需进行更改。

The DOM is a common interface for manipulating document structures. One of its design goals is that the Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without changes.

DOM interfaces

DOM 定义了若干 Java 接口。这里是最常见的接口 −

The DOM defines several Java interfaces. Here are the most common interfaces −

  1. Node − The base datatype of the DOM.

  2. Element − The vast majority of the objects you will deal with are Elements.

  3. Attr − Represents an attribute of an element.

  4. Text − The actual content of an Element or Attr.

  5. Document − Represents the entire XML document. A Document object is often referred to as a DOM tree.

Common DOM methods

当使用 DOM 时,有几种经常使用的方法 −

When you are working with the DOM, there are several methods that are used often −

  1. Document.getDocumentElement() − Returns the root element of the document.

  2. Node.getFirstChild() − Returns the first child of a given Node.

  3. Node.getLastChild() − Returns the last child of a given Node.

  4. Node.getNextSibling() − These methods return the next sibling of a given Node.

  5. Node.getPreviousSibling() − These methods return the previous sibling of a given Node.

  6. Node.getAttribute(attrName) − For a given Node, returns the attribute with the requested name.

DOM Parser - Parse XML Document

Steps to Use DOM

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

Following are the steps used while parsing a document using the DOM Parser.

  1. Import XML-related packages.

  2. Create a DocumentBuilder

  3. Create a Document from a file or stream

  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

Demo Example

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

Here is the input xml file we need to parse −

<?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();
      }
   }
}

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

The above program will generate the following result −

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

DOM Parser - Query XML Document

Demo Example

以下是我们必须查询的输入 xml 文件:

Here is the input xml file we need to query −

<?xml version = "1.0"?>
<cars>
   <supercars company = "Ferrari">
      <carname type = "formula one">Ferarri 101</carname>
      <carname type = "sports car">Ferarri 201</carname>
      <carname type = "sports car">Ferarri 301</carname>
   </supercars>

   <supercars company = "Lamborgini">
      <carname>Lamborgini 001</carname>
      <carname>Lamborgini 002</carname>
      <carname>Lamborgini 003</carname>
   </supercars>

   <luxurycars company = "Benteley">
      <carname>Benteley 1</carname>
      <carname>Benteley 2</carname>
      <carname>Benteley 3</carname>
   </luxurycars>
</cars>

Demo Example

QueryXmlFileDemo.java

package com.tutorialspoint.xml;

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;
import java.io.File;

public class QueryXmlFileDemo {

   public static void main(String argv[]) {

      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.print("Root element: ");
         System.out.println(doc.getDocumentElement().getNodeName());
         NodeList nList = doc.getElementsByTagName("supercars");
         System.out.println("----------------------------");
         for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("\nCurrent Element :");
            System.out.print(nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement = (Element) nNode;
               System.out.print("company : ");
               System.out.println(eElement.getAttribute("company"));
               NodeList carNameList =
                  eElement.getElementsByTagName("carname");
               for (int count = 0;
                  count < carNameList.getLength(); count++) {
                  Node node1 = carNameList.item(count);
                  if (node1.getNodeType() ==
                     node1.ELEMENT_NODE) {
                     Element car = (Element) node1;
                     System.out.print("car name : ");
                     System.out.println(car.getTextContent());
                     System.out.print("car type : ");
                     System.out.println(car.getAttribute("type"));
                  }
               }
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

Root element :cars
----------------------------

Current Element :supercars
company : Ferrari
car name : Ferarri 101
car type : formula one
car name : Ferarri 201
car type : sports car
car name : Ferarri 301
car type : sports car

Current Element :supercars
company : Lamborgini
car name : Lamborgini 001
car type :
car name : Lamborgini 002
car type :
car name : Lamborgini 003
car type :

DOM Parser - Create XML Document

Demo Example

以下是我们需要创建的 XML -

Here is the XML we need to create −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars><supercars company = "Ferrari">
   <carname type = "formula one">Ferrari 101</carname>
   <carname type = "sports">Ferrari 202</carname>
</supercars></cars>

Demo Example

CreateXmlFileDemo.java

package com.tutorialspoint.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;

public class CreateXmlFileDemo {

   public static void main(String argv[]) {

      try {
         DocumentBuilderFactory dbFactory =
         DocumentBuilderFactory.newInstance();
         DocumentBuilder dBuilder =
            dbFactory.newDocumentBuilder();
         Document doc = dBuilder.newDocument();

         // root element
         Element rootElement = doc.createElement("cars");
         doc.appendChild(rootElement);

         //  supercars element
         Element supercar = doc.createElement("supercars");
         rootElement.appendChild(supercar);

         // setting attribute to element
         Attr attr = doc.createAttribute("company");
         attr.setValue("Ferrari");
         supercar.setAttributeNode(attr);

         // carname element
         Element carname = doc.createElement("carname");
         Attr attrType = doc.createAttribute("type");
         attrType.setValue("formula one");
         carname.setAttributeNode(attrType);
         carname.appendChild(
         doc.createTextNode("Ferrari 101"));
         supercar.appendChild(carname);

         Element carname1 = doc.createElement("carname");
         Attr attrType1 = doc.createAttribute("type");
         attrType1.setValue("sports");
         carname1.setAttributeNode(attrType1);
         carname1.appendChild(
         doc.createTextNode("Ferrari 202"));
         supercar.appendChild(carname1);

         // write the content into xml file
         TransformerFactory transformerFactory =
         TransformerFactory.newInstance();
         Transformer transformer =
         transformerFactory.newTransformer();
         DOMSource source = new DOMSource(doc);
         StreamResult result =
         new StreamResult(new File("C:\\cars.xml"));
         transformer.transform(source, result);

         // Output to console for testing
         StreamResult consoleResult =
         new StreamResult(System.out);
         transformer.transform(source, consoleResult);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars><supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars></cars>

DOM Parser - Modify XML Document

Demo Example

以下是我们需要修改的输入 XML 文件 -

Here is the input xml file we need to modify −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars>
   <supercars company = "Ferrari">
      <carname type = "formula one">Ferrari 101</carname>
      <carname type = "sports">Ferrari 202</carname>
   </supercars>

   <luxurycars company = "Benteley">
      <carname>Benteley 1</carname>
      <carname>Benteley 2</carname>
      <carname>Benteley 3</carname>
   </luxurycars>
</cars>

Demo Example

ModifyXmlFileDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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;

public class ModifyXmlFileDemo {

public static void main(String argv[]) {

   try {
      File inputFile = new File("input.txt");
      DocumentBuilderFactory docFactory =
      DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder =
      docFactory.newDocumentBuilder();
      Document doc = docBuilder.parse(inputFile);
      Node cars = doc.getFirstChild();
      Node supercar = doc.getElementsByTagName("supercars").item(0);

      // update supercar attribute
      NamedNodeMap attr = supercar.getAttributes();
      Node nodeAttr = attr.getNamedItem("company");
      nodeAttr.setTextContent("Lamborigini");

      // loop the supercar child node
      NodeList list = supercar.getChildNodes();
      for (int temp = 0; temp < list.getLength(); temp++) {
         Node node = list.item(temp);
         if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) node;
            if ("carname".equals(eElement.getNodeName())){
               if("Ferrari 101".equals(eElement.getTextContent())){
                  eElement.setTextContent("Lamborigini 001");
               }
            if("Ferrari 202".equals(eElement.getTextContent()))
               eElement.setTextContent("Lamborigini 002");
            }
         }
      }

      NodeList childNodes = cars.getChildNodes();
      for(int count = 0; count < childNodes.getLength(); count++){
         Node node = childNodes.item(count);
         if("luxurycars".equals(node.getNodeName()))
            cars.removeChild(node);
         }

         // write the content on console
         TransformerFactory transformerFactory =
         TransformerFactory.newInstance();
         Transformer transformer = transformerFactory.newTransformer();
         DOMSource source = new DOMSource(doc);
         System.out.println("-----------Modified File-----------");
         StreamResult consoleResult = new StreamResult(System.out);
         transformer.transform(source, consoleResult);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

-----------Modified File-----------
<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars>
<supercars company = "Lamborigini">
<carname type = "formula one">Lamborigini 001</carname>
<carname type = "sports">Lamborigini 002</carname>
</supercars></cars>

SAX Parser - Overview

SAX(XML 简单 API)是基于事件的 XML 文档解析器。与 DOM 解析器不同,SAX 解析器不创建解析树。SAX 是一个 XML 流传输接口,这意味着使用 SAX 的应用程序会针对正在处理的 XML 文档接收事件通知,包括元素、属性以及从文档顶部开始、以 ROOT 元素的结束为结尾的顺序排列的时间。

SAX (the Simple API for XML) is an event-based parser for xml documents. Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed - an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.

  1. Reads an XML document from top to bottom, recognizing the tokens that make up a well-formed XML document.

  2. Tokens are processed in the same order as they appear in the document.

  3. Reports the application program, the nature of tokens that the parser has encountered as they occur.

  4. The application program provides an "event" handler that must be registered with the parser.

  5. As the tokens are identified, the callback methods in the handler are invoked with the relevant information.

When to use?

以下情况应使用 SAX 解析器 -

You should use a SAX parser when −

  1. You can process the XML document in a linear fashion from top to bottom.

  2. The document is not deeply nested.

  3. You are processing a very large XML document the DOM tree of which will consume a lot of memory. Typical DOM implementations use ten bytes of memory to represent one byte of XML.

  4. The problem to be solved involves only part of the XML document.

  5. Data is available as soon as it is seen by the parser, so SAX works well for an XML document that arrives over a stream.

Disadvantages of SAX

  1. We have no random access to an XML document since it is processed in a forwardonly manner.

  2. If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own.

ContentHandler Interface

此接口指定 SAX 解析器用于通知应用程序程序其看到的 XML 文档组件的回调方法。

This interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen.

  1. void startDocument() − Called at the beginning of a document.

  2. void endDocument() − Called at the beginning of a document.

  3. void startElement(String uri, String localName, String qName, Attributes atts) − Called at the beginning of an element.

  4. void endElement(String uri, String localName,String qName) − Called at the end of an element.

  5. void characters(char[] ch, int start, int length) − Called when the character data is encountered.

  6. void ignorableWhitespace( char[] ch, int start, int length) − Called when a DTD is present and ignorable whitespace is encountered.

  7. void processingInstruction(String target, String data) − Called when a processing instruction is recognized.

  8. void setDocumentLocator(Locator locator)) − Provides a Locator that can be used to identify positions in the document.

  9. void skippedEntity(String name) − Called when an unresolved entity is encountered.

  10. void startPrefixMapping(String prefix, String uri) − Called when a new namespace mapping is defined.

  11. void endPrefixMapping(String prefix) − Called when a namespace definition ends its scope.

Attributes Interface

此接口为处理连接到元素的属性指定方法。

This interface specifies methods for processing the attributes connected to an element.

  1. int getLength() − Returns the number of attributes.

  2. String getQName(int index)

  3. String getValue(int index)

  4. String getValue(String qname)

SAX Parser - Parse XML Document

Demo Example

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

Here is the input xml file we need to parse −

<?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>

UserHandler.java

package com.tutorialspoint.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;

   @Override
   public void startElement(String uri,
   String localName, String qName, Attributes attributes) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         String rollNo = attributes.getValue("rollno");
         System.out.println("Roll No : " + rollNo);
      } else if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      } else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }

   @Override
   public void endElement(String uri,
   String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         System.out.println("End Element :" + qName);
      }
   }

   @Override
   public void characters(char ch[], int start, int length) throws SAXException {
      if (bFirstName) {
         System.out.println("First Name: " + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName) {
         System.out.println("Last Name: " + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName) {
         System.out.println("Nick Name: " + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks) {
         System.out.println("Marks: " + new String(ch, start, length));
         bMarks = false;
      }
   }
}

SAXParserDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

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

      try {
         File inputFile = new File("input.txt");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         UserHandler userhandler = new UserHandler();
         saxParser.parse(inputFile, userhandler);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;

   @Override
   public void startElement(String uri,
      String localName, String qName, Attributes attributes) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         String rollNo = attributes.getValue("rollno");
         System.out.println("Roll No : " + rollNo);
      } else if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      } else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }

   @Override
   public void endElement(String uri,
      String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         System.out.println("End Element :" + qName);
      }
   }

   @Override
   public void characters(char ch[], int start, int length) throws SAXException {
      if (bFirstName) {
         System.out.println("First Name: " + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName) {
         System.out.println("Last Name: " + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName) {
         System.out.println("Nick Name: " + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks) {
         System.out.println("Marks: " + new String(ch, start, length));
         bMarks = false;
      }
   }
}

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

The above program will generate the following result −

Roll No : 393
First Name: Dinkar
Last Name: Kad
Nick Name: Dinkar
Marks: 85
End Element :student
Roll No : 493
First Name: Vineet
Last Name: Gupta
Nick Name: Vinni
Marks: 95
End Element :student
Roll No : 593
First Name: Jasvir
Last Name: Singh
Nick Name: Jazz
Marks: 90
End Element :student

SAX Parser - Query XML Document

Demo Example

以下是我们要查询学号的输入文本文件:393 -

Here is the input text file we need to Query for roll no: 393 −

<?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>

UserHandler.java

package com.tutorialspoint.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;
   String rollNo = null;

   @Override
   public void startElement(String uri,
      String localName, String qName, Attributes attributes) throws SAXException {

      if (qName.equalsIgnoreCase("student")) {
         rollNo = attributes.getValue("rollno");
      }
      if(("393").equals(rollNo) && qName.equalsIgnoreCase("student")){
         System.out.println("Start Element :" + qName);
      }
      if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      } else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }

   @Override
   public void endElement(String uri, String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         if(("393").equals(rollNo)
            && qName.equalsIgnoreCase("student"))
            System.out.println("End Element :" + qName);
      }
   }


   @Override
   public void characters(char ch[], int start, int length) throws SAXException {

      if (bFirstName && ("393").equals(rollNo)) {
         //age element, set Employee age
         System.out.println("First Name: " + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName && ("393").equals(rollNo)) {
         System.out.println("Last Name: " + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName && ("393").equals(rollNo)) {
         System.out.println("Nick Name: " + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks && ("393").equals(rollNo)) {
         System.out.println("Marks: " + new String(ch, start, length));
         bMarks = false;
      }
   }
}

SAXQueryDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

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

      try {
         File inputFile = new File("input.txt");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         UserHandler userhandler = new UserHandler();
         saxParser.parse(inputFile, userhandler);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;
   String rollNo = null;

   @Override
   public void startElement(String uri,
      String localName, String qName, Attributes attributes) throws SAXException {

      if (qName.equalsIgnoreCase("student")) {
         rollNo = attributes.getValue("rollno");
      }
      if(("393").equals(rollNo) &&
         qName.equalsIgnoreCase("student")){
         System.out.println("Start Element :" + qName);
      }
      if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      } else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }

   @Override
   public void endElement(String uri,
      String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         if(("393").equals(rollNo)
            && qName.equalsIgnoreCase("student"))
            System.out.println("End Element :" + qName);
      }
   }


   @Override
   public void characters(char ch[], int start, int length) throws SAXException {

      if (bFirstName && ("393").equals(rollNo)) {
         //age element, set Employee age
         System.out.println("First Name: " + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName && ("393").equals(rollNo)) {
         System.out.println("Last Name: " + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName && ("393").equals(rollNo)) {
         System.out.println("Nick Name: " + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks && ("393").equals(rollNo)) {
         System.out.println("Marks: " + new String(ch, start, length));
         bMarks = false;
      }
   }
}

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

The above program will generate the following result −

Start Element :student
First Name: Dinkar
Last Name: Kad
Nick Name: Dinkar
Marks: 85
End Element :student

SAX Parser - Create XML Document

使用 StAX 解析器创建 XML 比使用 SAX 解析器更好。请参阅 Java StAX Parser 部分以获取相同的信息。

It is better to use the StAX parser for creating XML than using the SAX parser. Please refer the Java StAX Parser section for the same.

SAX Parser - Modify XML Document

Demo Example

以下是我们必须在 </marks> tag 末尾附加 <Result>Pass<Result/> 以进行修改的输入 xml 文件。

Here is the input xml file we need to Modify by appending <Result>Pass<Result/> at the end of the </marks> tag.

<?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>

SAXModifyDemo.java

package com.tutorialspoint.xml;

import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.DefaultHandler;

public class SAXModifyDemo extends DefaultHandler {
   static String displayText[] = new String[1000];
   static int numberLines = 0;
   static String indentation = "";

   public static void main(String args[]) {

      try {
         File inputFile = new File("input.txt");
         SAXParserFactory factory =
         SAXParserFactory.newInstance();
         SAXModifyDemo obj = new SAXModifyDemo();
         obj.childLoop(inputFile);
         FileWriter filewriter = new FileWriter("newfile.xml");
         for(int loopIndex = 0; loopIndex < numberLines; loopIndex++){
            filewriter.write(displayText[loopIndex].toCharArray());
            filewriter.write('\n');
            System.out.println(displayText[loopIndex].toString());
         }
         filewriter.close();
      } catch (Exception e) {
         e.printStackTrace(System.err);
      }
   }

   public void childLoop(File input){
      DefaultHandler handler = this;
      SAXParserFactory factory = SAXParserFactory.newInstance();
      try {
         SAXParser saxParser = factory.newSAXParser();
         saxParser.parse(input, handler);
      } catch (Throwable t) {}
   }

   public void startDocument() {
      displayText[numberLines] = indentation;
      displayText[numberLines] += "<?xml version=\"1.0\" encoding=\""+
         "UTF-8" + "\"?>";
      numberLines++;
   }

   public void processingInstruction(String target, String data) {
      displayText[numberLines] = indentation;
      displayText[numberLines] += "<?";
      displayText[numberLines] += target;
      if (data != null && data.length() > 0) {
         displayText[numberLines] += ' ';
         displayText[numberLines] += data;
      }
      displayText[numberLines] += "?>";
      numberLines++;
   }

   public void startElement(String uri, String localName,
      String qualifiedName, Attributes attributes) {
      displayText[numberLines] = indentation;

      indentation += "    ";

      displayText[numberLines] += '<';
      displayText[numberLines] += qualifiedName;
      if (attributes != null) {
         int numberAttributes = attributes.getLength();
         for (int loopIndex = 0; loopIndex < numberAttributes;
            loopIndex++){
            displayText[numberLines] += ' ';
            displayText[numberLines] += attributes.getQName(loopIndex);
            displayText[numberLines] += "=\"";
            displayText[numberLines] += attributes.getValue(loopIndex);
            displayText[numberLines] += '"';
         }
      }
      displayText[numberLines] += '>';
      numberLines++;
   }

   public void characters(char characters[], int start, int length) {
      String characterData = (new String(characters, start, length)).trim();
      if(characterData.indexOf("\n") < 0 && characterData.length() > 0) {
         displayText[numberLines] = indentation;
         displayText[numberLines] += characterData;
         numberLines++;
      }
   }

   public void endElement(String uri, String localName, String qualifiedName) {
      indentation = indentation.substring(0, indentation.length() - 4);
      displayText[numberLines] = indentation;
      displayText[numberLines] += "</";
      displayText[numberLines] += qualifiedName;
      displayText[numberLines] += '>';
      numberLines++;

      if (qualifiedName.equals("marks")) {
         startElement("", "Result", "Result", null);
         characters("Pass".toCharArray(), 0, "Pass".length());
         endElement("", "Result", "Result");
      }
   }
}

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

The above program will generate the following result −

<?xml version = "1.0" encoding = "UTF-8"?>
<class>
   <student rollno = "393">
      <firstname>
         Dinkar
      </firstname>

      <lastname>
         Kad
      </lastname>

      <nickname>
         Dinkar
      </nickname>

      <marks>
         85
      </marks>

      <Result>
         Pass
      </Result>
   </student>

   <student rollno = "493">
      <firstname>
         Vineet
      </firstname>

      <lastname>
         Gupta
      </lastname>

      <nickname>
         Vinni
      </nickname>

      <marks>
         95
      </marks>

      <Result>
         Pass
      </Result>
   </student>

   <student rollno = "593">
      <firstname>
         Jasvir
      </firstname>

      <lastname>
         Singh
      </lastname>

      <nickname>
         Jazz
      </nickname>

      <marks>
        90
      </marks>

      <Result>
         Pass
      </Result>
   </student>
</class>

StAX Parser - Overview

StAX 是一个基于 JAVA 的 API,它以与 SAX 解析器类似的方式来解析 XML 文档。但这两个 API 之间有两个主要的差异点 −

StAX is a JAVA based API to parse XML document in a similar way as SAX parser does. But there are two major points of difference between the two APIs −

  1. StAX is a PULL API whereas, SAX is a PUSH API. It means in case of StAX parser, the client application needs to ask the StAX parser to get information from XML whenever it needs but in case of the SAX parser, client application is required to get information when the SAX parser notifies the client application that information is available.

  2. StAX API can read as well as write XML documents. Using SAX API, xml can be only read.

以下是 StAX API 的特性:

Following are the features of StAX API −

  1. Reads an XML document from top to bottom, recognizing the tokens that make up a well-formed XML document.

  2. Tokens are processed in the same order as they appear in the document.

  3. Reports the application program on the nature of tokens that the parser has encountered as they occur.

  4. The application program provides an "event" reader which acts as an iterator and iterates over the event to get the required information. Another reader available is the "cursor" reader which acts as a pointer to xml nodes.

  5. As the events are identified, xml elements can be retrieved from the event object and can be processed further.

When to use?

您应该在以下情况下使用 StAX 解析器:

You should use a StAX parser when −

  1. You can process the XML document in a linear fashion from top to bottom.

  2. The document is not deeply nested.

  3. You are processing a very large XML document the DOM tree of which will consume too much memory. Typical DOM implementations use ten bytes of memory to represent one byte of XML.

  4. The problem to be solved involves only part of the XML document.

  5. Data is available as soon as it is seen by the parser, so StAX works well for an XML document that arrives over a stream.

Disadvantages of SAX

  1. We have no random access to an XML document since it is processed in a forward-only manner.

  2. If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own.

XMLEventReader Class

该类提供事件的迭代器,可用在解析 XML 文档时用于迭代遇到的事件

This class provides the iterator of events which can be used to iterate over events as they occur while parsing the XML document

  1. StartElement asStartElement() − Used to retrieve value and attributes of element.

  2. EndElement asEndElement() − Called at the end of an element.

  3. Characters asCharacters() − Can be used to obtain characters such as CDATA, whitespace, etc.

XMLEventWriter Class

此接口指定用于创建事件的方法。

This interface specifies methods for creating an event.

  1. add(Event event) − Adds event containing elements to XML.

XMLStreamReader Class

这个类提供事件迭代器,它可用于在解析 XML 文档期间迭代事件。

This class provide iterator of events which can be used to iterate over events as they occur while parsing the XML document

  1. int next() − Used to retrieve next event.

  2. boolean hasNext() − Used to check further events exists or not

  3. String getText() − Used to get text of an element

  4. String getLocalName() − Used to get name of an element

XMLStreamWriter Class

此接口指定用于创建事件的方法。

This interface specifies methods for creating an event.

  1. writeStartElement(String localName) − Adds start element of a given name.

  2. writeEndElement(String localName) − Adds end element of a given name.

  3. writeAttribute(String localName, String value) − Writes attribute to an element.

StAX Parser - Parse XML Document

Demo Example

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

Here is the input xml file we need to parse −

<?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>

StAXParserDemo.java

package com.tutorialspoint.xml;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXParserDemo {
   public static void main(String[] args) {
      boolean bFirstName = false;
      boolean bLastName = false;
      boolean bNickName = false;
      boolean bMarks = false;
      try {
         XMLInputFactory factory = XMLInputFactory.newInstance();
         XMLEventReader eventReader =
         factory.createXMLEventReader(new FileReader("input.txt"));

         while(eventReader.hasNext()){
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()){
               case XMLStreamConstants.START_ELEMENT:
                  StartElement startElement = event.asStartElement();
                  String qName = startElement.getName().getLocalPart();
                  if (qName.equalsIgnoreCase("student")) {
                     System.out.println("Start Element : student");
                     Iterator<Attribute> attributes = startElement.getAttributes();
                     String rollNo = attributes.next().getValue();
                     System.out.println("Roll No : " + rollNo);
                  } else if (qName.equalsIgnoreCase("firstname")) {
                     bFirstName = true;
                  } else if (qName.equalsIgnoreCase("lastname")) {
                     bLastName = true;
                  } else if (qName.equalsIgnoreCase("nickname")) {
                     bNickName = true;
                  } else if (qName.equalsIgnoreCase("marks")) {
                     bMarks = true;
                  }
                  break;
               case XMLStreamConstants.CHARACTERS:
                  Characters characters = event.asCharacters();
                  if(bFirstName){
                     System.out.println("First Name: " + characters.getData());
                     bFirstName = false;
                  }
                  if(bLastName){
                     System.out.println("Last Name: " + characters.getData());
                     bLastName = false;
                  }
                  if(bNickName){
                     System.out.println("Nick Name: " + characters.getData());
                     bNickName = false;
                  }
                  if(bMarks){
                     System.out.println("Marks: " + characters.getData());
                     bMarks = false;
                  }
                  break;
               case XMLStreamConstants.END_ELEMENT:
                  EndElement endElement = event.asEndElement();
                  if(endElement.getName().getLocalPart().equalsIgnoreCase("student")){
                     System.out.println("End Element : student");
                     System.out.println();
                  }
                  break;
            }
         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (XMLStreamException e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

Start Element : student
Roll No : 393
First Name: Dinkar
Last Name: Kad
Nick Name: Dinkar
Marks: 85
End Element : student

Start Element : student
Roll No : 493
First Name: Vineet
Last Name: Gupta
Nick Name: Vinni
Marks: 95
End Element : student

Start Element : student
Roll No : 593
First Name: Jasvir
Last Name: Singh
Nick Name: Jazz
Marks: 90
End Element : student

StAX Parser - Query XML Document

Demo Example

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

Here is the input xml file we need to parse −

<?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>

StAXParserDemo.java

package com.tutorialspoint.xml;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXQueryDemo {
   public static void main(String[] args) {
      boolean bFirstName = false;
      boolean bLastName = false;
      boolean bNickName = false;
      boolean bMarks = false;
      boolean isRequestRollNo = false;
      try {
         XMLInputFactory factory = XMLInputFactory.newInstance();
         XMLEventReader eventReader = factory.createXMLEventReader(
            new FileReader("input.txt"));

         String requestedRollNo = "393";
         while(eventReader.hasNext()){
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()){
               case XMLStreamConstants.START_ELEMENT:
                  StartElement startElement = event.asStartElement();
                  String qName = startElement.getName().getLocalPart();
                  if (qName.equalsIgnoreCase("student")) {
                     Iterator<Attribute> attributes = startElement.getAttributes();
                     String rollNo = attributes.next().getValue();
                     if(rollNo.equalsIgnoreCase(requestedRollNo)){
                        System.out.println("Start Element : student");
                        System.out.println("Roll No : " + rollNo);
                        isRequestRollNo = true;
                     }
                  } else if (qName.equalsIgnoreCase("firstname")) {
                     bFirstName = true;
                  } else if (qName.equalsIgnoreCase("lastname")) {
                     bLastName = true;
                  } else if (qName.equalsIgnoreCase("nickname")) {
                     bNickName = true;
                  } else if (qName.equalsIgnoreCase("marks")) {
                     bMarks = true;
                  }
                  break;
               case XMLStreamConstants.CHARACTERS:
                  Characters characters = event.asCharacters();
                  if(bFirstName && isRequestRollNo){
                     System.out.println("First Name: " + characters.getData());
                     bFirstName = false;
                  }
                  if(bLastName && isRequestRollNo){
                     System.out.println("Last Name: " + characters.getData());
                     bLastName = false;
                  }
                  if(bNickName && isRequestRollNo){
                     System.out.println("Nick Name: " + characters.getData());
                     bNickName = false;
                  }
                  if(bMarks && isRequestRollNo){
                     System.out.println("Marks: " + characters.getData());
                     bMarks = false;
                  }
                  break;
               case  XMLStreamConstants.END_ELEMENT:
                  EndElement endElement = event.asEndElement();
                  if(endElement.getName().getLocalPart().equalsIgnoreCase("student") && isRequestRollNo){
                     System.out.println("End Element : student");
                     System.out.println();
                     isRequestRollNo = false;
                  }
                  break;
            }
         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (XMLStreamException e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

Start Element : student
Roll No : 393
First Name: Dinkar
Last Name: Kad
Nick Name: Dinkar
Marks: 85
End Element : student

StAX Parser - Create XML Document

Demo Example

Here is the XML we need to create −

Here is the XML we need to create −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
   <cars><supercars company = "Ferrari">
   <carname type = "formula one">Ferrari 101</carname>
   <carname type = "sports">Ferrari 202</carname>
</supercars></cars>

Demo Example

StAXCreateXMLDemo.java

package com.tutorialspoint.xml;

import java.io.IOException;
import java.io.StringWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class StAXCreateXMLDemo {
   public static void main(String[] args) {
      try {
         StringWriter stringWriter = new StringWriter();

         XMLOutputFactory xMLOutputFactory = XMLOutputFactory.newInstance();
         XMLStreamWriter xMLStreamWriter = xMLOutputFactory.createXMLStreamWriter(stringWriter);

         xMLStreamWriter.writeStartDocument();
         xMLStreamWriter.writeStartElement("cars");

         xMLStreamWriter.writeStartElement("supercars");
         xMLStreamWriter.writeAttribute("company", "Ferrari");

         xMLStreamWriter.writeStartElement("carname");
         xMLStreamWriter.writeAttribute("type", "formula one");
         xMLStreamWriter.writeCharacters("Ferrari 101");
         xMLStreamWriter.writeEndElement();

         xMLStreamWriter.writeStartElement("carname");
         xMLStreamWriter.writeAttribute("type", "sports");
         xMLStreamWriter.writeCharacters("Ferrari 202");
         xMLStreamWriter.writeEndElement();

         xMLStreamWriter.writeEndElement();
         xMLStreamWriter.writeEndDocument();

         xMLStreamWriter.flush();
         xMLStreamWriter.close();

         String xmlString = stringWriter.getBuffer().toString();

         stringWriter.close();

         System.out.println(xmlString);

      } catch (XMLStreamException e) {
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<cars><supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars></cars>

StAX Parser - Modify XML Document

Demo Example

为了运行此示例,您应该在应用程序的类路径中有 jdom.jar。下载 jdom-2.0.5.zip.

In order to run this example, you should have jdom.jar in your application’s classpath. Download jdom-2.0.5.zip.

以下是我们要修改的 XML -

Here is the XML we need to modify −

<?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

StAXModifyDemo.java

package com.tutorialspoint.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

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

      try {
         XMLInputFactory factory = XMLInputFactory.newInstance();
         XMLEventReader eventReader =
            factory.createXMLEventReader(
               new FileReader("input.txt"));
         SAXBuilder saxBuilder = new SAXBuilder();
         Document document = saxBuilder.build(new File("input.txt"));
         Element rootElement = document.getRootElement();
         List<Element> studentElements = rootElement.getChildren("student");
         while(eventReader.hasNext()){
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()){
               case XMLStreamConstants.START_ELEMENT:
               StartElement startElement = event.asStartElement();
               String qName = startElement.getName().getLocalPart();

               if (qName.equalsIgnoreCase("student")) {
                  Iterator<Attribute> attributes = startElement.getAttributes();
                  String rollNo = attributes.next().getValue();
                  if(rollNo.equalsIgnoreCase("393")){
                     //get the student with roll no 393
                     for(int i=0;i < studentElements.size();i++){
                        Element studentElement = studentElements.get(i);
                        if(studentElement.getAttribute("rollno").getValue().equalsIgnoreCase("393")){
                           studentElement.removeChild("marks");
                           studentElement.addContent(new Element("marks").setText("80"));
                        }
                    }
                  }
               }
               break;
            }
         }
         XMLOutputter xmlOutput = new XMLOutputter();
         // display xml
         xmlOutput.setFormat(Format.getPrettyFormat());
         xmlOutput.output(document, System.out);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (XMLStreamException e) {
         e.printStackTrace();
      } catch (JDOMException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

The above program will generate the following result −

<student rollno = "393">
   <firstname>Dinkar</firstname>
   <lastname>Kad</lastname>
   <nickname>Dinkar</nickname>
   <marks>80</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>