sax解析xml案例

前端之家收集整理的这篇文章主要介绍了sax解析xml案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有一个xml文档

<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
	<书>
		<书名 name="xxxx">java web</书名>
		<作者>张大</作者>
		<价格>102</价格>
	</书>
	<书>
		<书名>spring</书名>
		<作者>呜呜</作者>
		<价格>55</价格>
	</书>
</书架>

解析所有节点
package cn.itcast.sax;

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 Demo1 {
	public static void main(String[] args) throws Exception {
		
		//1.创建工厂
		SAXParserFactory saxP = SAXParserFactory.newInstance();
		
		//2.得到解析器
		SAXParser parser = saxP.newSAXParser();
		
		//3.得到读取器
		XMLReader xmlR = parser.getXMLReader();
		
		//4.设置内容处理器
		xmlR.setContentHandler(new ParseS());
		//5.读取xml内容
		xmlR.parse("src/book.xml");
	}
}

//得到xml文档的内容
class ParseS implements ContentHandler{

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startPrefixMapping(String prefix,String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException {
		System.out.println("<"+qName+">");
		for(int i=0; i<atts.getLength(); i++){
			String name = atts.getQName(i);
			String value = atts.getValue(i);
			System.out.println(name+": "+value);
		}
	}

	@Override
	public void endElement(String uri,String qName)
			throws SAXException {
		System.out.println("</"+qName+">");
		
	}

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

	@Override
	public void ignorableWhitespace(char[] ch,int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String target,String data)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}
	
}

获取指定标签的值
package cn.itcast.sax;

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 Demo2 {

	public static void main(String[] args) throws Exception{
		//1.创建工厂
		SAXParserFactory saxP = SAXParserFactory.newInstance();
		
		//2.得到解析器
		SAXParser parser = saxP.newSAXParser();
		
		//3.得到读取器
		XMLReader xmlR = parser.getXMLReader();
		
		//4.设置内容处理器
		xmlR.setContentHandler(new TagHandler());
		//5.读取xml内容
		xmlR.parse("src/book.xml");
		
	}

}

//获取指定标签的值
class TagHandler extends DefaultHandler{
	
	private String currentTag;  //记录当前解析到的标签
	private int needNum = 2;   //记录想获取第几个标签
	private int currentNum;
	
	@Override
	public void startElement(String uri,Attributes attributes) throws SAXException {
		currentTag = qName;
		if("作者".equals(currentTag)){
			currentNum++;
		}
	}

	@Override
	public void endElement(String uri,String qName)
			throws SAXException {
		currentTag = null;
	}

	@Override
	public void characters(char[] ch,int length)
			throws SAXException {
		if("作者".equals(currentTag) && currentNum==needNum){
			System.out.println(new String(ch,length));
		}
	}
	
}
原文链接:https://www.f2er.com/xml/297958.html

猜你在找的XML相关文章