1. 程式人生 > >帶DTD 的XML 用SAX進行解析

帶DTD 的XML 用SAX進行解析

    我們知道,在java中,解析的xml檔案的外掛很多,dom4j,jdom等等,但是它們都是基於dom和sax兩種形式就行解析的,我們明白,dom是解析小檔案的,一次性的把檔案載入到記憶體中進行解析,sax是分步進行的,只有用到的時候,才會到硬碟上去載入xml檔案,適合大檔案xml解析,下面我們就來書寫一下sax對xml的解析例子。

     使用過sax解析xml的朋友都知道,它需要指明一個處理類來進行xml的解析處理,為了方便,就可以直接把處理類和解析類放入同一個類中來進行

package test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
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 SAXParse extends DefaultHandler {

	private Stack<InputStream> stack = new Stack<InputStream>();
	public void parse(String path) throws ParserConfigurationException, SAXException, IOException{
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		parserFactory.setValidating(true);
		SAXParser parser = parserFactory.newSAXParser();
		InputStream is = SAXParse.class.getClassLoader().getResourceAsStream(path);
		if(is.available() > 0){
			stack.push(is);
		}
		URL url = SAXParse.class.getClassLoader().getResource(path);
		File file = new File(url.getFile());
		parser.parse(is, this,file.getAbsolutePath());
	}

	public void endDocument() throws SAXException {
		if(!stack.empty()){
			InputStream is = stack.pop();
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
					throw new SAXException(e.getMessage());
				}
			}
		}
	}

	public void startDocument() throws SAXException {
	}

	public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
		System.out.print("<"+name+"  ");
		for(int i = 0 ; i < attributes.getLength() ; i++){
			String qname = attributes.getQName(i);
			String value = attributes.getValue(i);
			System.out.print(qname+"=\""+value+"\" ");
		}
		System.out.println("></"+name+">");
	}
	
	
	public static void main(String[] args){
		SAXParse p = new SAXParse();
		try {
			p.parse("test/test.xml");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

dtd檔案,儲存為test.dtd,和test.xml儲存到同一個目錄下面

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT root (person*)>
<!ELEMENT person (baby*)>
<!ATTLIST person idcard ID #REQUIRED>
<!ATTLIST person name CDATA #REQUIRED>
<!ATTLIST person sex (1|0|F|M) "f">
<!ATTLIST person remark CDATA "">
<!ATTLIST baby name CDATA #REQUIRED>
<!ATTLIST baby age CDATA "0">
<!ATTLIST baby sex (1|0|F|M) "f">


xml檔案test.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root SYSTEM "test.dtd" >
<root>
	<person idcard="1234567890" name="張三" remark="remark" sex="1">
		<baby name="大兒子" age="2" sex="M" />
		<baby name="二女兒" age="1" sex="F" />
	</person>
</root>