51.md 7.4 KB
Newer Older
W
wizardforcel 已提交
1
# Java SAX 解析器 – 阅读 XML 示例
W
wizardforcel 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

> 原文: [https://howtodoinjava.com/xml/sax-parser-read-xml-example/](https://howtodoinjava.com/xml/sax-parser-read-xml-example/)

**SAX 解析器**[**XML**](//howtodoinjava.com/category/xml/ "xml tag") 的简单 API 已经存在很多年了,最初由 [David Megginson](http://www.megginson.com/) 领导开发。 千年。 那时,您不得不从 David 的个人网站下载 Java 版本的 SAX。 在最终添加到 Java Standard Edition 1.4 中之前,它已发展为 **[SAX 项目](http://www.saxproject.org/)**

SAX 是 XML 的流接口,这意味着使用 SAX 的应用程序从文档的顶部开始,以顺序的时间接收到有关正在处理的 XML 文档的事件通知的元素和属性,并以关闭文档的结尾结束。 根元素。 这意味着它在线性时间内处理 XML 的效率非常高,而不会对系统内存提出过多要求。

让我们创建一个演示程序,以便**使用 SAX 解析器**读取 xml 文件以全面理解。

## 1.准备要解析的 xml 文件

该 xml 文件包含 xml 属性以及 xml 元素。

```java
<users>
	<user id="100">
		<firstname>Tom</firstname>
		<lastname>Hanks</lastname>
	</user>
	<user id="101">
		<firstname>Lokesh</firstname>
		<lastname>Gupta</lastname>
	</user>
	<user id="102">
		<firstname>HowToDo</firstname>
		<lastname>InJava</lastname>
	</user>
</users>

```

## 2.创建模型类

```java
package com.howtodoinjava.xml.sax;

/**
 * Model class. Its instances will be populated using SAX parser.
 * */
public class User
{
	//XML attribute id
	private int id;
	//XML element fisrtName
	private String firstName;
	//XML element lastName
	private String lastName;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return this.id + ":" + this.firstName +  ":" +this.lastName ;
	}
}

```

## 3.通过扩展 DefaultParser 构建处理程序

下面的代码为解析处理程序。 我在代码注释中添加了其他信息。 不过,您有任何疑问吗,请给我留言。

```java
package com.howtodoinjava.xml.sax;

import java.util.ArrayList;
import java.util.Stack;

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

public class UserParserHandler extends DefaultHandler
{
	//This is the list which shall be populated while parsing the XML.
    private ArrayList userList = new ArrayList();

    //As we read any XML element we will push that in this stack
    private Stack elementStack = new Stack();

    //As we complete one user block in XML, we will push the User instance in userList
    private Stack objectStack = new Stack();

    public void startDocument() throws SAXException
    {
        //System.out.println("start of the document   : ");
    }

    public void endDocument() throws SAXException
    {
        //System.out.println("end of the document document     : ");
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
    	//Push it in element stack
        this.elementStack.push(qName);

        //If this is start of 'user' element then prepare a new User instance and push it in object stack
        if ("user".equals(qName))
        {
            //New User instance
        	User user = new User();

            //Set all required attributes in any XML element here itself
            if(attributes != null &amp;&amp; attributes.getLength() == 1)
            {
            	user.setId(Integer.parseInt(attributes.getValue(0)));
            }
            this.objectStack.push(user);
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException
    {
    	//Remove last added  element
        this.elementStack.pop();

        //User instance has been constructed so pop it from object stack and push in userList
        if ("user".equals(qName))
        {
            User object = this.objectStack.pop();
            this.userList.add(object);
        }
    }

    /**
     * This will be called everytime parser encounter a value node
     * */
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        String value = new String(ch, start, length).trim();

        if (value.length() == 0)
        {
            return; // ignore white space
        }

        //handle the value based on to which element it belongs
        if ("firstName".equals(currentElement()))
        {
            User user = (User) this.objectStack.peek();
            user.setFirstName(value);
        }
        else if ("lastName".equals(currentElement()))
        {
            User user = (User) this.objectStack.peek();
            user.setLastName(value);
        }
    }

    /**
     * Utility method for getting the current element in processing
     * */
    private String currentElement()
    {
        return this.elementStack.peek();
    }

    //Accessor for userList object
    public ArrayList getUsers()
    {
    	return userList;
    }
}

```

## 4\. SAX 解析器读取 XML 文件

```java
package com.howtodoinjava.xml.sax;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class UsersXmlParser
{
	public ArrayList parseXml(InputStream in)
	{
		//Create a empty link of users initially
		ArrayList<user> users = new ArrayList</user><user>();
		try
		{
			//Create default handler instance
			UserParserHandler handler = new UserParserHandler();

			//Create parser from factory
			XMLReader parser = XMLReaderFactory.createXMLReader();

			//Register handler with parser
			parser.setContentHandler(handler);

			//Create an input source from the XML input stream
			InputSource source = new InputSource(in);

			//parse the document
			parser.parse(source);

			//populate the parsed users list in above created empty list; You can return from here also.
			users = handler.getUsers();

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

		}
		return users;
	}
}

```

## 5)测试 SAX 解析器

让我们编写一些代码来测试我们的处理程序是否真正起作用。

```java
package com.howtodoinjava.xml.sax;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class TestSaxParser
{
	public static void main(String[] args) throws FileNotFoundException
	{
		//Locate the file
		File xmlFile = new File("D:/temp/sample.xml");

		//Create the parser instance
		UsersXmlParser parser = new UsersXmlParser();

		//Parse the file
		ArrayList users = parser.parseXml(new FileInputStream(xmlFile));

		//Verify the result
		System.out.println(users);
	}
}

Output:
[100:Tom:Hanks, 101:Lokesh:Gupta, 102:HowToDo:InJava]

```

[**Download sourcecode for this post**](https://drive.google.com/file/d/0B7yo2HclmjI4Y1REbUt1aV9lZlk/view?usp=drive_web "download source code for sax parser")

学习愉快!