1. 程式人生 > >xml jasp sax解析

xml jasp sax解析

xml檔案:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 書架 [
	<!ELEMENT 書架 (書+)>
	<!ELEMENT 書 (書名,作者,售價)>
	<!ELEMENT 書名 (#PCDATA)>
	<!ELEMENT 作者 (#PCDATA)>
	<!ELEMENT 售價 (#PCDATA)>
]>

<書架>
	<書>
		<書名 name="xxx">鋼鐵是怎樣煉成的</書名>
		<作者>尼·奧斯特洛夫斯基 </作者>
		<售價>16.5</售價>
	</書>
	<書>
		<書名>假如給我三天光明</書名>
		<作者>海倫·凱勒 </作者>
		<售價>17.6</售價>
	</書>
</書架>

一、得到xml文件中的所有類容;

package cn.sax;

import java.io.IOException;

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

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;


//sax解析xml文件
public class Demo1 {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//1.建立解析工廠
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		
		
		//2.得到解析器
		SAXParser sp = factory.newSAXParser();
		//3.得到讀取器
		XMLReader reader = sp.getXMLReader();
		//4.設定內容處理器
		reader.setContentHandler(new ListHandler());
		//5.讀取xml類容。
		
		reader.parse("src/book.xml");
		
	}

}

//得到xml文件中的所有類容;
class ListHandler implements ContentHandler{
	public void startElement(String uri, String localName, String name,
			Attributes atts) throws SAXException {
		System.out.println("<"+name+">");
		for (int i=0;atts!=null && i<atts.getLength();i++){
			String index = atts.getQName(i);  //得到屬性名
			String value = atts.getValue(i);  //得到屬性值
			System.out.println(index + "=" + value);
		}
		
	}
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		System.out.println(new String(ch,start,length));
		
	}
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		System.out.println("</"+name+">");
		
	}


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

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

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

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

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



	
	

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

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

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









 二、獲取指定標籤的值

package cn.sax;

import java.io.IOException;

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

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


//sax解析xml文件
public class Demo2 {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//1.建立解析工廠
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		
		
		//2.得到解析器
		SAXParser sp = factory.newSAXParser();
		//3.得到讀取器
		XMLReader reader = sp.getXMLReader();
		//4.設定內容處理器
		reader.setContentHandler(new TagValueHandler());
		//5.讀取xml類容。
		
		reader.parse("src/book.xml");
		
	}

}

//獲取指定標籤的值
class TagValueHandler extends DefaultHandler{
	private String currenttag;   //用來記住當前的標籤名
	private int needNumber =2;   //想獲取的第幾個作者標籤的值
	private int currentNumber;  //當前解析到的是第幾個
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currenttag = qName;
		if(currenttag.equalsIgnoreCase("作者")){
			currentNumber++;
		}
	}

	

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if("作者".equals(currenttag) && currentNumber==needNumber){
			System.out.println(new String(ch,start,length));
		}
	}
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		currenttag=null;
	}
	
	
	
}









三、Sax解析案例javabean封裝xml文件資料物件

package cn.sax;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

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


//sax解析xml文件
public class Demo3 {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//1.建立解析工廠
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		
		
		//2.得到解析器
		SAXParser sp = factory.newSAXParser();
		//3.得到讀取器
		XMLReader reader = sp.getXMLReader();
		//4.設定內容處理器.
		BeanListHandler handler = new BeanListHandler();
		reader.setContentHandler(handler);
		//5.讀取xml類容。
		
		reader.parse("src/book.xml");
		List<Book> list = handler.getList();

		Book book=list.get(1);
		System.out.println(book.getName());
		System.out.println(book.getAuthor());
		System.out.println(book.getPrice());
	}

}

//獲取指定標籤的值
class BeanListHandler extends DefaultHandler{
	private List list = new ArrayList();
	
	private String currentTag;
	private Book book;
	
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentTag = qName;
		if("書".equals(currentTag)){
			book = new Book();
			
		}
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if("書名".equals(currentTag)){
			String name = new String(ch,start,length);
			book.setName(name);
		}
		if("作者".equals(currentTag)){
			String author = new String(ch,start,length);
			book.setAuthor(author);
		}
		if("售價".equals(currentTag)){
			String price = new String(ch,start,length);
			book.setPrice(price);
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("書")){
			list.add(book);
			book = null;
		}
		currentTag=null;
			
	}

	public List getList() {
		return list;
	}

}