首页 > 代码库 > 用javabean封装xml文档数据
用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; } }
//学习自方立勋老师视频教程
用javabean封装xml文档数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。