JavaWeb - XML解析 - SAX

sax解析

2.xml
<?xml version="1.0" encoding="GBK" standalone="no" ?>
<addresslist>
	<linkman>
		<name id="001">W_Jp</name>
		<email>271953489@qq.com</email>
	</linkman>
	<linkman>
		<name id="002">W_Cc</name>
		<email>109604484@qq.com</email>
	</linkman>
	<linkman>
		<name id="003">C_Jm</name>
		<email>879451036@qq.com</email>
	</linkman>
</addresslist>
demo.java
import org.xml.sax.* ;
import org.xml.sax.helpers.* ;
public class demo extends DefaultHandler{
	public void startDocument()
                   throws SAXException{
		System.out.println("<?xml version=\"1.0\" encoding=\"GBK\" ?>") ;
	}
	public void startElement(String uri,
							String localName,
							String qName,
							Attributes attributes)
					 throws SAXException{
		System.out.print("<") ;
		System.out.print(qName) ;
		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 endElement(String uri,
						  String localName,
						  String qName)
				   throws SAXException{
		System.out.print("<") ;
		System.out.print(qName) ;
		System.out.print(">") ;
	}
	public void endDocument()
                 throws SAXException{
		System.out.print("文档结束...") ;
	}
	public void characters(char[] ch,
					  int start,
					  int length)
			   throws SAXException{
		System.out.println(new String(ch, start, length)) ;
	}
}
TestSax.java
import java.io.* ;
import javax.xml.parsers.* ;
public class TestSax {
	public static void main(String args []) throws Exception{
		// 建立SAX的解析工厂
		SAXParserFactory factory = SAXParserFactory.newInstance() ;
		SAXParser parser = factory.newSAXParser() ;
		parser.parse("e:" + File.separator + "02.xml", new demo()) ;
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值