Java SAX DefaultHandler

Source: Internet
Author: User

org.xml.sax.helpers.DefaultHandlerThe class is the base class for ' listeners ' in SAX 2.0.

As shown briefly in the first text on SAX in this tutorial, you create a subclass of and DefaultHandler override certain inherited Methods in this subclass. In this text I'll show a very simple example of a DefaultHandler  subclass, which just prints out detail about the XML file. Here is the code:

 Public classSaxhandlerextendsDefaultHandler { Public voidStartdocument ()throwssaxexception {System.out.println ("Start Document:"); }     Public voidEnddocument ()throwssaxexception {System.out.println ("End Document:"); }     Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {System.out.println ("Start element:" +qName); }     Public voidendElement (String uri, String localname, string qName)throwssaxexception {System.out.println ("End element:" +qName); }     Public voidCharacters (CharCh[],intStartintlength)throwssaxexception {System.out.println ("Start characters:" +NewString (CH, start, length)); }}

When you run the this code, with the this file as input:

<root>    <child>        <grandchild>text 1</grandchild>    </child>    < child>        <grandchild>text 2</grandchild>    </child></root>

... you get the following output printed to the System.out:

Start document   :    start element    : root    characters       :    start element    :    Child Characters       :    start element    : grandchild    characters       1    end element      : Grandchild    characters       :    end element      : child    characters       :    start element    : Child    characters       :    start element    : grandchild    characters       2    End Element      : Grandchild    characters       :    end element      : child    characters       :    end Element      : Root    End document     :

SAXParserthe sequence in which the calls the corresponding methods SaxHandler in the instance and when processing the XML F Ile shown above.

You could have noticed that sometimes the characters() method prints out a line break. This is because the method was called by the and the characters() SAXParser whitespace characters that was located between the end of The parent element begin tag, and the child begin tag. In and words, the white space characters marked here using dots (...):

<root> ..... <child>    </child>

There is also sometimes whitespace characters located after the end of an element end tag, and until the beginning of the Next sibling tag, or the beginning of the end tag of the the parent element.

Processing instructions

The DefaultHandler class also have a method for when XML processing instructions is found in the XML file. Here's how this method looks:

 Public void processinginstruction (string target, string data) throws saxexception {}

You don't very often use processing instructions, so I-won ' t get into more detail on it here. Now so you know it's here and you can play with it yourself.

Exceptions

The DefaultHandler class has three methods-can override to handle exceptions encountered during the XML parsing. Here they is:

 Public void throws saxexception {}  Public void throws saxexception {}  Public void throws saxexception {}
Java SAX DefaultHandler
by Jakob JenkovConnect with Me:rate Article:share Article:tweet

Table of Contents
    • Processing instructions
    • Exceptions
    • Additional Methods

org.xml.sax.helpers.DefaultHandlerThe class is the base class for ' listeners ' in SAX 2.0.

As shown briefly in the first text on SAX in this tutorial, you create a subclass of and DefaultHandler override certain inherited Methods in this subclass. In this text I'll show a very simple example of a DefaultHandler subclass, which just prints out detail about the XML file. Here is the code:

public class Saxhandler extends DefaultHandler {public    void Startdocument () throws Saxexception {        SYSTEM.OUT.PRINTLN ("Start document   :");    }    public void Enddocument () throws Saxexception {        System.out.println ("End document     :");    }    public void Startelement (string uri, String localname,        string qName, Attributes Attributes)    throws saxexception {        System.out.println ("Start element    :" + qName);    }    public void EndElement (string uri, String localname, String qName)    throws saxexception {        System.out.println (" End element      : "+ qName);    }    public void characters (char ch[], int start, int length)    throws saxexception {        System.out.println ("Start Characters: "+            new String (CH, start, length));}    }

When you run the this code, with the this file as input:

<root>    <child>        <grandchild>text 1</grandchild>    </child>    <child >        <grandchild>text 2</grandchild>    </child></root>

... you get the following output printed to the System.out:

    Start document   :    start element    : root    characters       :    start element    :    Child Characters       :    start element    : grandchild    characters       : Text 1    end element      : Grandchild    characters       :    end element      : child    characters       :    start element    : Child    characters       :    start element    : grandchild    characters       : Text 2    end element      : Grandchild    characters       :    end element      : child    characters       :    end Element      : Root    End document     :

SAXParserthe sequence in which the calls the corresponding methods SaxHandler in the instance and when processing the XML fil e shown above.

You could have noticed that sometimes the characters() method prints out a line break. This is because the method was called by the and the characters() SAXParser whitespace characters that was located between the end of The parent element begin tag, and the child begin tag. In and words, the white space characters marked here using dots (...):

<root> ... .... <child>    </child>

There is also sometimes whitespace characters located after the end of an element end tag, and until the beginning of the Next sibling tag, or the beginning of the end tag of the the parent element.

Processing instructions

The DefaultHandler class also have a method for when XML processing instructions is found in the XML file. Here's how this method looks:

public void ProcessingInstruction (string target, String data) throws Saxexception {}

You don't very often use processing instructions, so I-won ' t get into more detail on it here. Now so you know it's here and you can play with it yourself.

Exceptions

The DefaultHandler class has three methods-can override to handle exceptions encountered during the XML parsing. Here they is:

public void Warning (saxparseexception e) throws saxexception {}public void error (Saxparseexception e) throws Saxexception {}public void FatalError (Saxparseexception e) throws saxexception {}

Let's say that the parser encounters a illegal XML entity (like &notLegal;). SAXParser fatalError() The'll and call the method, before breaking the parsing.

If A less dangerous error occurs, the could SAXParser just call the error() or warning() method. That's the collect all the errors in a list, and return them all at once, instead of one by one, as they is met.

Additional Methods

The have more DefaultHandler methods can override. Check out the JavaDoc for more details on those methods.

Java SAX DefaultHandler

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.