码迷,mamicode.com
首页 > 编程语言 > 详细

用javabean封装xml文档数据

时间:2014-11-19 01:27:01      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:xml   javabean   java   sax解析   处理器   

//book.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
	<book id="1">
		<name>冰与火之歌</name>
		<author>乔治马丁</author>
		<year>2014</year>
		<price>89</price>
		<language>English</language>	
	</book>
	
	<book id="2">
		<name>安徒生童话</name>
		<year>2004</year>
		<price>77</price>
		<language>English</language>
	</book>
</bookstore>

//Books.java

package com.huowolf.handler;

public class Books {
	private String name;
	private String author;
	private int year;
	private double price;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public String getName() {
		return name;
	}
	public String getAuthor() {
		return author;
	}
	public int getYear() {
		return year;
	}
	public double getPrice() {
		return price;
	}
	
	
}
//Demo.java

package com.huowolf.handler;

/*
 * 用javabean封装xml文档数据
 */
import java.util.ArrayList;
import java.util.List;

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

	public static void main(String[] args) throws Exception {
		//获取一个解析工厂
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		//通过factory获取解析器
		SAXParser parser = factory.newSAXParser();
		
		//得到读取器
		XMLReader reader = parser.getXMLReader();
		
		//设置内容处理器
		BeanListHandler handler = new BeanListHandler();
		reader.setContentHandler(handler);
		
		//读取xml文档内容
		reader.parse("books.xml");
		
		List<Books> list = handler.getBooks();
		System.out.println(list);
		
		System.out.println(list.get(0).getAuthor());

	}
}


//把xml文档中的每一本书封装到一个book对象,并把多个book对象放在一个list集合中返回

class BeanListHandler extends DefaultHandler {
	private List<Books> list= new ArrayList<Books>();
	private String currentTag;
	private Books  book;
	
	@Override
	public void startDocument() throws SAXException {
			System.out.println("开始解析");
	}
		
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentTag = qName;
		if(currentTag.equals("book")) {
			book = new Books();
		}
	}
	
	@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
		
			if("name".equals(currentTag)) {
				String name = new String(ch, start, length);
				book.setName(name);
			}
			
			if("author".equals(currentTag)) {
				String author = new String(ch, start, length);
				book.setAuthor(author);
			}
			
			if("year".equals(currentTag)) {
				int year = Integer.parseInt(new String(ch, start, length));
				book.setYear(year);
			}
				
			if("price".equals(currentTag)) {
				double price = Double.parseDouble(new String(ch, start, length));
				book.setPrice(price);
			}
		}
	


	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
			//System.out.println(qName);
			if(qName.equals("book")) {
				list.add(book);
				book = null;
			}
			currentTag = null;
	}

	//返回list集合
	public List<Books> getBooks() {
		return list;
	}

	
	
}



//学习自方立勋老师视频教程

用javabean封装xml文档数据

标签:xml   javabean   java   sax解析   处理器   

原文地址:http://blog.csdn.net/huolang_vip/article/details/41254075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!