Java parsing XML documents (2): sax read xml file export excel

1. Concept
SAX (Simple API for XML), event-based XML Simple API, parsing XML documents is one of the two API.

2. Sax parsing

1. Use to the package:

javax.xml.parsers: parser factory tools
org.xml.sax: provides the core SAX API

For example:
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

2. Resolution Process

First of all build our parser, we need to inherit org.xml.sax.helpers.DefaultHandler class, covering one approach. Then to obtain SAX factory object, from the factory to obtain SAX parsing, the parser and parse the object (xml file) link to begin parsing, after the success of the last access to the properties can be resolved.

Additional information:
In reading the xml file parsing, when they read the corresponding content, corresponds to the event will be thrown by the ContentHandler method calls its corresponding response. DefaultHandler implements this interface ContentHandler, so DefaultHandler to serve as the default SAX2 event handler base class.

Common to the method:
(1) characters (char [] ch, int start, int length)

Receive notification of character data elements available in this approach, the <xxx> </ xxx> added to the value between the properties.

(2) startElement (String uri, String localName, String qName, Attributes attributes)
Receive notification of the beginning elements of the parameter in the uri is the name of the domain, localName is the label name, qName is a modification of the label prefix, attributes that this tag contains a list of attributes.

(3) endElement (String uri, String localName, String qName)
Receive notification of the end element.

3. Export EXCEL

jxl.jar: provides a java tool for operational excel forms library

For example:

import jxl .*;
import jxl.write .*;
import jxl.write.Number;

Code Reference:

WritableWorkbook plan = Workbook.createWorkbook(new File("plan.xls"));  
 

// Create first paging, from  0 Start  

                
WritableSheet sheet = book.createSheet("name1", 0); 


// Parameters, respectively.  : Columns, rows,  , Value, this represents the fill Cap.  1 Column 1 row of cells  , Subscripts start at 0  


sheet.addCell(new Label(0, 0, "TYPE_ID")); 

...

plan.write();   
plan.close();


4. Examples of the code

Reference to the annexes.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Java parsing XML documents (2): sax read xml file export excel

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries