about me

LexicalHandlerExample

| 2 thg 4, 2010

Create Project : DemoSAXinJAXP
Create file XML : Student.xml

 

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE StudentList[

<!ELEMENT StudentList (Student)+>

<!--- Put your DTDDoc comment here. -->

<!ELEMENT Student (Age|Name)*>

<!ATTLIST Student

    Gender CDATA #IMPLIED

    StudentID CDATA #IMPLIED

  >

 

<!--- Put your DTDDoc comment here. -->

<!ELEMENT Name (#PCDATA)>

 

<!--- Put your DTDDoc comment here. -->

<!ELEMENT Age (#PCDATA)>

<!ENTITY class "C0709J">

]>

<StudentList>

    <!--Mo ta sinh vien-->

    <Student StudentID="S01" Gender="Male"  >

        <Name>Nguyen Van Tuyen &class;

         <!--Mo ta sinh vien-->

        <![CDATA[<greeting>Hello, world!</greeting>]]>

        </Name>

        <Age>23</Age>

    </Student>

    <Student StudentID="S02" Gender="FeMale" >

        <Name>Kieu Thi Nga </Name>

        <Age>20</Age>

    </Student>

    <Student StudentID="S03" Gender="FeMale" >

        <Name>Nguyen Thi Hoa </Name>

        <Age>21</Age>

    </Student>

</StudentList>


Create class : LexicalHandlerExample


/*
 * LexicalHandlerExample.java
 *
 * This example demonstrates the implementations of LexicalHandler interface methods and use of EntityResolver
 *
 * Copyright �2007 Aptech Software Limited. All rights reserved.
 */
package app;


import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.IOException;
import org.xml.sax.helpers.DefaultHandler;


publicclass LexicalHandlerExample extends DefaultHandler implements LexicalHandler {
    // variable declarations for holding the count of entities, CDATA and comments found in the document


    static int countEntity = 0;
    static int countCDATA = 0;
    static int countComment = 0;


    public static void main(String[] args) {


        // set up the parser
        XMLReader parser;
        try {
            // obtain an XMLReader instance for parsing
            parser = XMLReaderFactory.createXMLReader();
        } catch (SAXException e) {
            System.err.println("Error: could not locate a parser.");
            return;
        }
        try {
            // Create class instance for lexical handler
            LexicalHandlerExample handler = new LexicalHandlerExample();
            // Inform the parser to send the lexical events to the specified lexical event handler
            parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
            // Inform the parser that you would like to resolve entities using the specified handler
            parser.setEntityResolver(handler);
        } catch (SAXNotRecognizedException e) {
            System.err.println("Installed XML parser does not provide lexical events...");
            return;
        } catch (SAXNotSupportedException e) {
            System.err.println("Operation not supported");
            return;
        }
        try {
            // Start parsing
            parser.parse("src\\app\\Student.xml");
        } catch (SAXParseException e) {
            // well-formedness error
            System.out.println("W3Csample.xml is not well formed.");
            System.out.println(e.getMessage() + " at line " + e.getLineNumber() + ", column " + e.getColumnNumber());
        } catch (SAXException e) {
            // some other kind of error
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not read W3Csample.xml because of the IOException " + e);
        }
        System.out.println(countEntity + " entities found");
        System.out.println(countCDATA + " CDATA items found");
        System.out.println(countComment + " comments found");
    }


    public void startDTD(String name, String publicId, String systemId) throws SAXException {
        System.out.println("DTD found");
    }


    public void endDTD() throws SAXException {
    }


    public void startEntity(String name) throws SAXException {
        countEntity++;
    }


    public void endEntity(String name) throws SAXException {
        System.out.println("Entity Name: "+name);
    }


    public void startCDATA() throws SAXException {
        countCDATA++;
    }


    public void endCDATA() throws SAXException {
    }


    public void comment(char[] ch, int start, int length) throws SAXException {
        countComment++;
        String s=new String(ch, start, length);
        System.out.println(s);
    }
    /*
     * The parser calls this method to allow the application to
     * resolve the found entity
     */


    @Override
    public InputSource resolveEntity(String publicID, String systemID) {
        System.out.println("Resolved Entity - publicID: " + publicID + "  SystemID: " + systemID);
        return null;
    }
}

0 nhận xét:

Đăng nhận xét