SAX 解析XML

SAX 解析 XML

/加入 :
D:\Soft\jar\Xerces-J-bin.2.11.0\xerces-2_11_0\xercesImpl.jar

SAXFilter.java

package com.itheima.xml;

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

public class SAXFilter extends XMLFilterImpl {  //扩展相应过滤器实现操作XML文档

	private String currentElement;  // 当前元素
	
	public SAXFilter(XMLReader parent){
		super(parent);
	}

	//过滤掉 元素 flower 的开始事件
	@Override
	public void startElement(String uri, String elementName, String fullName,
			Attributes atts) throws SAXException {
		// TODO Auto-generated method stub
		if(!"flower".equals(elementName)){  
			super.startElement(uri, elementName, fullName, atts);
		}
		
	}
	//过滤掉 元素 flower 的结束事件
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(!"flower".equals(localName)){
			super.endElement(uri, localName, qName);
		}
		
	}

	//过滤掉 flower 中的内容
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		if(!"flower".equals(currentElement)){
			super.characters(ch, start, length);
		}
	}
	
}


SAXHandler.java
package com.itheima.xml;

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

public //扩展相应的类使用SAX读取XML文件
class SAXHandler extends DefaultHandler{

	//处理文档解析开始事件
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("<?xml version=\"1.0\" encodin='gb2312'?>");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		System.out.print("<"+qName);
		int len=attributes.getLength();
		for (int i = 0; i < len; i++) {
			System.out.print(" ");
			System.out.print(attributes.getQName(i));
			System.out.print("=\"");
			System.out.print(attributes.getValue(i));
			System.out.print("\"");
		}
		System.out.print(">");
			
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.print("</"+qName+">");
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.print(new String(ch,start,length));
	}

	//处理 接戏中产生处理指令事件
	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.print("<?"+target+" "+ data+" ?>");
	}
	
	
	
}



TextSAXParseXML.java
package com.itheima.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class TextSAXParseXML {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName="F:/poem.xml"; //已经存在的文件
		try {
			System.out.println("==========================1.运用扩展XMLFilterImpl过滤累实现SAX解析XML文件");
			threeCreateSAXParser(fileName);
			System.out.println();
			System.out.println("==========================2.运用扩展DefaultHandler类实现SAX解析XML文件");
			oneCreateSAXParser(fileName);
			System.out.println();
			System.out.println("==========================3.运用创建ContentHandler类实现SAX解析XML文件");
			twoCreateSAXParser(fileName);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.exit(1); //退出  0:表示 正常退出 1:表示非正常 
		}
	}
	
	
	public static void oneCreateSAXParser(String fileName){
		SAXParserFactory spf=SAXParserFactory.newInstance();
		try {
			SAXParser sp=spf.newSAXParser();  //创建SAX解析器
			sp.parse(new File(fileName),new SAXHandler()); //开始解析文档
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	//第二种构造解析器的方法
	public static void twoCreateSAXParser(String fileName) throws SAXException, IOException{
		XMLReader reader=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
		
		ContentHandler contentHandler=new SAXHandler();
		reader.setContentHandler(contentHandler);
		reader.parse(fileName);
	}
	
	public static void threeCreateSAXParser(String fileName) throws SAXException, IOException{
		XMLReader reader=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
		XMLFilter filter=new SAXFilter(reader); //初始化过滤器
		
		DefaultHandler defaultHandler=new SAXHandler();
		filter.setContentHandler(defaultHandler); //为过滤后的事件流设置defaultHandler
		filter.setErrorHandler(defaultHandler);//为过滤后的事件流设置ErrorHandler
		filter.parse(fileName); //开始解析XML文件
	}

}


你可能感兴趣的:(xml,sax)