用javabean封装xml文档数据

//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;
	}

	
	
}



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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。