Encapsulate xml document data with javabean

Source: Internet
Author: User

Encapsulate xml document data with javabean

// Book. xml

 
 
  
   
    
Song of ice and fire
   George Martin
   
    
2014
   
   
    
89
   
   
    
English
   
  
  
   
    
Andersen fairy tale
   
   
    
2004
   
   
    
77
   
   
    
English
   
  
 

// 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;/** Use javabean to encapsulate xml document data */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 {// get a parsing factory SAXParserFactory factory = SAXParserFactory. newInstance (); // get the parser SAXParser parser = factory through factory. newSAXParser (); // get the reader XMLReader reader = parser. getXMLReader (); // set the content processor BeanListHandler handler = new BeanListHandler (); reader. setContentHandler (handler); // read the xml document content reader. parse ("books. xml "); List
 
  
List = handler. getBooks (); System. out. println (list); System. out. println (list. get (0 ). getAuthor () ;}// encapsulate each book in the xml document into a book object, and put multiple book objects in a list set to return class BeanListHandler extends DefaultHandler {private List
  
   
List = new ArrayList
   
    
(); Private String currentTag; private Books book; @ Overridepublic void startDocument () throws SAXException {System. out. println ("START Parsing") ;}@ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {currentTag = qName; if (currentTag. equals ("book") {book = new Books () ;}@ Overridepublic 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) ;}@ Overridepublic 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;} // returns the public list of List sets.
    
     
GetBooks () {return list ;}}
    
   
  
 



// Learn from Fang lixun's video tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.