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")); 



4. Examples of the code

Reference to the annexes.

分类:Java 时间:2010-03-22 人气:385
blog comments powered by Disqus


  • Java programming language commonly used in the method of updating XML documents 2010-09-28

    Quoted from This paper briefly discusses the Java programming language update XML documents in the four commonly used methods, and analysis of the merits of the four methods. Secondly, the article

  • Java parsing XML document (1): DOM 2010-02-21

    1. Overview DOM (Document Object Module), based on the document object tree and node types. In a real project, had encountered large xml files need to be addressed, probably more than 200 M, load the XML file will be reported memory leak java.lang.Ou

  • Use Dom4J parsing XML (2) 2010-06-08

    3. Generate xml file package domj4Parser; import java.util.List; import java.util.Iterator; import *; / / Import dom4j API class import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Attribute; import org.dom4j.Node; import o

  • JavaScript parsing XML summary 2010-08-16

    XML parsing many ways, such as data island, W3C DOM methods, used here to sum up JavaScript parsing xml string. function loadXml(data){ //IE Loads the xml string if(navigator.userAgent.indexOf('MSIE') >= 0) { // Load the xml string (IE The way of loa

  • Java parsing XML documents four methods 2010-05-07

    1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy allows the developer to find sp

  • How To Read and Write XML Documents with GDataXML 2012-07-03

    In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comments section suggested writing a post on how to use an XML library to read and write XML documents, create your own objects based on the documents, an

  • Using dom4j to create, edit XML documents and format the output and the Chinese to solve the problem 2010-08-22

    package com.holen.dom4j; import; import; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import

  • Database holds a large number of XML documents (that exist) 2010-10-19

    Relational database to store large XML documents can cause many problems. Solve these problems is the first step in a deep understanding of the parameters of an XML document. Then the correct design appropriate solutions. Solutions may include: a ser

  • Real-time stock data interface Daquan 2010-03-30 Baidu Space | Baidu Home | Login o0 Heaven de wedding gown 0ohappycool Home blog Photos | Profile | Friends view the article Real-time stock data interface 2009-12-15 16:36 Daquan Daqua

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 2.282 (s). 10 q(s)