JavaWeb学习笔记——SAX解析

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

//=================================================
// File Name       :	MySAX
//------------------------------------------------------------------------------
// Author          :	Common

//类名:MySAX
//属性:
//方法:
public class MySAX extends DefaultHandler{				//定义SAX解析器
	public void startDocument() throws SAXException{		//文档开始
		System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	}
	
	public void endDocument() throws SAXException{		//文档结束
		System.out.println("\n 文档读取结束。。。");
	}
	
	public void startElement(String uri,String localName,String name,Attributes attributes) throws SAXException{
		System.out.print("<");
		System.out.print(name);
		if(attributes != null){
			for(int i=0;i<attributes.getLength();i++){
				System.out.print(" "+attributes.getQName(i)+"=\""+attributes.getValue(i)+"\"");
			}
			System.out.print(">");
		}
	}
	
	public void character(char[] ch,int start,int lenght) throws SAXException{
		System.out.print(new String(ch,start,lenght));
	}
	
	public void endElement(String uri,String localName,String name) throws SAXException{
		System.out.print("</");
		System.out.print(name);
		System.out.print(">");
	}
	
}

 

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

//=================================================
// File Name       :	testSAX
//------------------------------------------------------------------------------
// Author          :	Common

public class testSAX {

	public static void main(String[] args) throws Exception{
		// TODO 自动生成的方法存根
		//建立SAX解析工厂
		SAXParserFactory factory = SAXParserFactory.newInstance();
		//构造解析器
		SAXParser parser = factory.newSAXParser();
		//解析XML,使用HANDLER
		parser.parse("/home/common/software/coding/HelloWord/JavaWeb/bin/dom_name.xml", new MySAX());
	}

}

 

转载于:https://www.cnblogs.com/tonglin0325/p/5343338.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值