用sax解析器来解析xml文档

解析下面的xml文档

<?xml version="1.0" encoding="UTF-8"?>
<exam>
     <student examcard="1" idcard="001">
	     <name>李华</name>
	     <location>上海</location>
	     <grade>90</grade>
     </student>
     <student examcard="2" idcard="002">
	     <name>张三</name>
	     <location>北京</location>
	     <grade>80</grade>
     </student>
     <student examcard="3" idcard="003">
	     <name>王</name>
	     <location>武汉</location>
	     <grade>90</grade>
     </student>
</exam>

用下面的程序解析

import java.io.IOException;

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

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class Dome1 {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//创建sax的解析工厂
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//得到解析器
		SAXParser sax=factory.newSAXParser();
		//得到读取器
		XMLReader read=sax.getXMLReader();
		//设置事件处理器
		read.setContentHandler(new handler());
		
		//读取本地资源
		//read.parse("G:\\JAVA  TEST\\SaxTest\\src\\student.xml");
		read.parse("src/student.xml");
	}
	
}

class handler implements ContentHandler{
	
	
	public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
	
		System.out.println("<"+"name"+">");
	}



	public void characters(char[] ch, int start, int length) throws SAXException {
		
		System.out.println(new String(ch,start,length));

	}
	
	public void endElement(String arg0, String arg1, String arg2) throws SAXException {
		System.out.println("</"+"name"+">");
	
				
	}



	public void endDocument() throws SAXException {

		
	}


	

	public void endPrefixMapping(String arg0) throws SAXException {

		
	}


	public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {

		
	}


	public void processingInstruction(String arg0, String arg1) throws SAXException {
	
	}

	@Override
	public void setDocumentLocator(Locator arg0) {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO 自动生成的方法存根
		
	}

	
	@Override
	public void startPrefixMapping(String arg0, String arg1) throws SAXException {
		// TODO 自动生成的方法存根
		
	}
}

选择性解析

import java.io.IOException;

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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Dome {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
	{
		SAXParserFactory factory=SAXParserFactory.newInstance();
		SAXParser sax=factory.newSAXParser();
		XMLReader read=sax.getXMLReader();
		//设置事件处理器
		read.setContentHandler(new ContentHandler());
		
		read.parse("src/student.xml");

	}
}
class ContentHandler extends DefaultHandler{
	//记住当前解析到的是什么标签
	private String contenttag;
	private int neednumber=2;         //设置要获取的标签索引,这里要获取第二个
	private int currentnumber;        //设置解析器解析当前标签的索引
	public void startElement(String arg0, String arg1, String name, Attributes arg3) throws SAXException {
		contenttag=name;
		if("name".equals(contenttag)){
			currentnumber++;            //记录解析标签的索引
		}
		
	}
	public void characters(char[] ch, int start, int length) throws SAXException {
		//若解析当前标签     等于     要获取的标签,则获取该name
		if("name".equals(contenttag)&¤tnumber==neednumber){
			System.out.println(new String(ch,start,length));
		}
	}
	public void endElement(String arg0, String arg1, String arg2) throws SAXException {
		contenttag=null;
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值