A case study of the SAX parsing technique for XML parsing

Source: Internet
Author: User

Java code:

Package com.xushouwei.xml;

import java.io.File;

import java.io.IOException;

import Java.text.DateFormat;

import Java.text.SimpleDateFormat;

import javax.xml.parsers.ParserConfigurationException;

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;

/**

* Sax Parsing XML data

Principle

* The SAX parser parses the XML document from the beginning of the XML document, and the colleague determines whether the currently parsed part (element, attribute, or element content) is required to be recorded and saved, based on the event handler already defined. Relative to Dom parsing, his little thing takes up a little bit of memory to find faster.

* @author Xu Shouwei

*

*/

Public class Saxparsedemo {

Public Static void Main (string[] args) {

Creates a parse XML Document object, which is stored in the root directory of the E-disk Article.xml

File xmlfile=new file ("E:\\article.xml");

Create a SAXParserFactory object, created by a singleton pattern, saxparserfactory object equivalent to the creation of the SAXParser parser, through the saxparserfactory.newinstance () method to create a SAXParserFactory object

SAXParserFactory factory=saxparserfactory. newinstance ();

Try {

Get SAXParser object from SAXParserFactory

SAXParser Parser=factory.newsaxparser ();

Parsing File contents

Try {

Parser.parse (xmlfile, new Mysaxhandler ());

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

} catch (Parserconfigurationexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (Saxexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

}

Because Sax parsing is event-driven, we define a time listener object here, Mysaxhandler, which can inherit the DefaultHandler class, similar to the event listener in swing, and here's a look at

class Mysaxhandler extends DefaultHandler

{

Format Date format

Static DateFormat DateFormat=new simpledateformat ("Yyyy-mm-dd HH:mm:ss");

Content

Private String content;

The character in an element when an event occurs

@Override

Public void characters (char[] ch, int start, int length)

throws saxexception {

TODO auto-generated Method stub

content=new String (CH, start, length);

}

triggered when parsing to an end tag of an element

@Override

Public void endElement (string uri, String localname, String qName)

throws saxexception {

TODO auto-generated Method stub

If the title

if ("title". Equals (QName))

{

System. out. println ("title:" +content);

}

If the author

Else if ("Author". Equals (QName))

{

System. out. println ("+content");

}

If it's a mail

Else if ("Email". Equals (QName))

{

System. out. println ("Mail:" +content);

}

If the content

Else if ("Body". Equals (QName))

{

System. out. println ("content:" +content);

}

If it is a date

Else if ("Date". Equals (QName))

{

System. out. println ("date:" +content);

}

}

triggered when parsing to the start tag of an element

@Override

Public void startelement (string uri, String localname, String qName,

Attributes Attributes) throws saxexception {

TODO auto-generated Method stub

if ("article". Equals (QName))

{

If the node name is article, the output Article element property category

System. out. println ("\ r \ n found an article, Category:" +attributes.getvalue ("category") + ".");

}

}

}

XML code:

<?xml version= "1.0" encoding= "GB2312"?>
<articles>
<article category= "Android" >
<title>android Learning Route </title>
<author> Xu Shouwei </author>
<email>[email protected]</email>
<date>2016-05-20</date>
</article>
<article category= "Javaweb" >
<title>javaweb Study Notes </title>
<author>Jasxu</author>
<email>[email protected]</email>
<date>2016-05-21</date>
</article>
</articles>

A case study of the SAX parsing technique for XML parsing

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.