XML-based SAX Parsing Model and xmlsax Parsing

Source: Internet
Author: User

XML-based SAX Parsing Model and xmlsax Parsing

DOM parsing maps the entire XML file to the tree structure in the Document. When a large file is encountered, the memory usage is large and the search is slow.

SAX is a solution to this problem. The SAX Parser parses the XML file from the starting position, and according to the defined event processor, to determine whether it is necessary to record and store the currently resolved part.

Import java. io. file; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; public class SaxParser {public static void main () {File xmlFile = new File ("E: \ article. xml "); SAXParserFactoryfactory = SAXParserFactory. newInstance (); try {SAXParserparser = factory. newSAXParser (); parser. parse (xmlFile, newMySaxHandler ();} catch (Exception e) {e. printstacktrace () ;}} import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; public class MySaxHandler extends DefaultHandler {static DateFormat formater = newSimpleDateFormat ("yyyy-MM-dd"); private String content; // override the charaters method in DefaultHandler, to receive notifications of character data in an element, perform this operation after the startElement or endElement method is executed @ Override publicvoid charaters (char [] ch, int start, int length) throws SAXException {content = newString (ch, start, length) ;}// @ Override publicvoid endElement (String uri, String localName, String qName) is triggered when the end tag of the element is parsed) throws SAXException {if ("title ". equals (qName) System. out. println ("title:" + content); else if ("author ". equals (qName) System. out. println ("Author:" + content); else if ("email ". equals (qName) System. out. println ("Email:" + content); else if ("date ". equals (qName) System. out. println ("Date:" + content) ;}// triggered when the start tag of the parsed element @ Override publicvoid startElement (String uri, String localName, String qName Attitudesattributes) throws SAXException {if ("article ". equals (qName) {System. out. println ("category:" + attributes. getValue ("category "));}}}

 

DefaultHandler class

When parsing XML data, you need to create a listener object for parsing. Generally, You can implement this by inheriting the DefaultHandler class.

Void characters (char [] ch, int start, int length)

Receives notifications of character data in the element.

Void endDocument ()

Receive the notification of document termination.

Void endElement (String uri, String localName, String qName)

Receives notifications of the end of an element.

Void endPrefixMapping (String prefix)

Receives notifications about the end of namespace ing.

Void error (SAXParseException e)

Receive notifications of recoverable parser errors.

Void fatalError (SAXParseException e)

Report serious XML parsing errors.

Void ignorableWhitespace (char [] ch, int start, int length)

Blank notifications can be ignored in the content of the receiving element.

Void notationDecl (String name, StringpublicId, String systemId)

Receive comments.

Void processingInstruction (String target, String data)

Receive notifications of processing commands.

InputSource resolveEntity (String publicId, String systemId)

Parses external entities.

Void setDocumentLocator (Locator locator)

The Locator object that receives document events.

Void skippedEntity (String name)

Receives notifications of skipped objects.

Void startDocument ()

Receive notifications starting with a document.

Void startElement (String uri, StringlocalName, String qName, Attributes attributes)

Receives notifications starting with an element.

Uri-namespace URI. If the element does not have any namespace URI or is not performing namespace processing, it is a null string.

LocalName-local name (without prefix). If namespace processing is not in progress, it is a null string.

QName-a qualified name (with a prefix). If the qualified name is unavailable, it is a null string.

Attributes-attributes appended to an element. If there is no attribute, it will be an empty Attributes object.

Void startPrefixMapping (String prefix, Stringuri)

Receives notifications about the start of namespace ing.

Void unparsedEntityDecl (String name, StringpublicId, String systemId, String notationName)

Receive notifications of unresolved entity declarations.

Void warning (SAXParseException e)

Receive a notification from the parser.

Attributes

Int getIndex (String qName)

Search for the index of an attribute using an XML qualified (prefix) Name.

Int getIndex (String uri, String localName)

Search for the index of an attribute using the namespace name.

Int getLength ()

Returns the number of attributes in the list.

String getLocalName (int index)

Search for the local name of an attribute through an index.

String getQName (int index)

Search for the XML limitation (prefix) name of an attribute through an index.

String getType (int index)

Search for the attribute type through the index.

String getType (String qName)

You can use an XML prefix to specify the attribute type.

String getType (String uri, String localName)

Search for the attribute type based on the namespace name.

String getURI (int index)

Search for the namespace URI of an attribute through an index.

String getValue (int index)

Search for attribute values through indexes.

String getValue (String qName)

Search for attribute values by using an XML qualified (prefix) Name.

String getValue (String uri, String localName)

Search for the attribute value based on the namespace name.

Related Article

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.