DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Extending DefaultHandler

  1. #1
    David Rancour Guest

    Extending DefaultHandler


    Hi all,

    I am writing a very basic example of using the SAX api and am running into
    trouble. The class below compiles just fine, but when I try to execute the
    code I get a crazy error. Here is the code and the error:

    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import java.io.*;

    public class xmlEcho extends DefaultHandler
    {
    public void startDocument() throws SAXException
    {
    System.out.println("SAX Event: START Document");
    }

    public void endDocument() throws SAXException
    {
    System.out.println("SAX Event: END Document");
    }

    public void startElement(String namespaceURI,String localName, String qName,
    Attributes attrs) throws SAXException
    {
    System.out.println("SAX Event: START Element["
    +localName+"]");
    for (int i=0;i < attrs.getLength() ;i++ )
    {
    System.out.println(" ATTRIBUTE "+
    attrs.getLocalName(i)+" VALUE "+
    attrs.getValue(i));
    }
    }

    public void endElement(String namespaceURI,String localName,
    String qName) throws SAXException
    {
    System.out.println("SAX Event: END Element["
    +localName+"]");
    }

    public void characters(char ch[],int start, int length) throws SAXException
    {
    System.out.print("SAX Event: CHARACTERS[");
    try
    {
    OutputStreamWriter outw = new OutputStreamWriter(System.out);
    outw.write(ch,start,length);
    outw.flush();
    } catch (Exception e)
    {
    e.printStackTrace();
    }

    System.out.println("]");
    }

    public static void main(String args[])
    {
    System.out.println("Example1 SAX Events");
    try
    {
    XMLReader xr = XMLReaderFactory.createXMLReader();
    xr.setContentHandler(new xmlEcho());
    xr.parse(new InputSource(new FileReader("c:\\web.xml")));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    *********ERROR Text
    Exception in thread "main" java.lang.NoClassDefFoundError: org/xml/sax/helpers/DefaultHandler
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    How could the program be extending the DefaultHandler class, compile perfectly
    then have a problem locating the class?

    Any idea's about what I'm doing wrong here? Thanks alot, I am really stuck.

    David Rancour

  2. #2
    Rajkamal Gopinath Guest

    Re: Extending DefaultHandler


    Hi,

    Do you have Jaxp classes version 1.1 in your CLASSPATH ?

    Regards

    Raj


    "David Rancour" <david_rancour@hotmail.com> wrote:
    >
    >Hi all,
    >
    >I am writing a very basic example of using the SAX api and am running into
    >trouble. The class below compiles just fine, but when I try to execute the
    >code I get a crazy error. Here is the code and the error:
    >
    >import org.xml.sax.*;
    >import org.xml.sax.helpers.*;
    >import java.io.*;
    >
    >public class xmlEcho extends DefaultHandler
    >{
    > public void startDocument() throws SAXException
    > {
    > System.out.println("SAX Event: START Document");
    > }
    >
    > public void endDocument() throws SAXException
    > {
    > System.out.println("SAX Event: END Document");
    > }
    >
    > public void startElement(String namespaceURI,String localName, String qName,
    > Attributes attrs) throws SAXException
    > {
    > System.out.println("SAX Event: START Element["
    > +localName+"]");
    > for (int i=0;i < attrs.getLength() ;i++ )
    > {
    > System.out.println(" ATTRIBUTE "+
    > attrs.getLocalName(i)+" VALUE "+
    > attrs.getValue(i));
    > }
    > }
    >
    > public void endElement(String namespaceURI,String localName,
    > String qName) throws SAXException
    > {
    > System.out.println("SAX Event: END Element["
    > +localName+"]");
    > }
    >
    > public void characters(char ch[],int start, int length) throws SAXException
    > {
    > System.out.print("SAX Event: CHARACTERS[");
    > try
    > {
    > OutputStreamWriter outw = new OutputStreamWriter(System.out);
    > outw.write(ch,start,length);
    > outw.flush();
    > } catch (Exception e)
    > {
    > e.printStackTrace();
    > }
    >
    > System.out.println("]");
    > }
    >
    > public static void main(String args[])
    > {
    > System.out.println("Example1 SAX Events");
    > try
    > {
    > XMLReader xr = XMLReaderFactory.createXMLReader();
    > xr.setContentHandler(new xmlEcho());
    > xr.parse(new InputSource(new FileReader("c:\\web.xml")));
    > }
    > catch (Exception e)
    > {
    > e.printStackTrace();
    > }
    > }
    >}
    >
    >*********ERROR Text
    >Exception in thread "main" java.lang.NoClassDefFoundError: org/xml/sax/helpers/DefaultHandler
    > at java.lang.ClassLoader.defineClass0(Native Method)
    > at java.lang.ClassLoader.defineClass(Unknown Source)
    > at java.security.SecureClassLoader.defineClass(Unknown Source)
    > at java.net.URLClassLoader.defineClass(Unknown Source)
    > at java.net.URLClassLoader.access$100(Unknown Source)
    > at java.net.URLClassLoader$1.run(Unknown Source)
    > at java.security.AccessController.doPrivileged(Native Method)
    > at java.net.URLClassLoader.findClass(Unknown Source)
    > at java.lang.ClassLoader.loadClass(Unknown Source)
    > at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    > at java.lang.ClassLoader.loadClass(Unknown Source)
    > at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    >
    >How could the program be extending the DefaultHandler class, compile perfectly
    >then have a problem locating the class?
    >
    >Any idea's about what I'm doing wrong here? Thanks alot, I am really stuck.
    >
    >David Rancour



  3. #3
    Adam Guest

    Re: Extending DefaultHandler


    Hey man,

    You just need to add this to your classpath var:
    X:\<path to your project,package, or class>\<name of the Parsers project,
    package, or class>;


    Hope this helps,

  4. #4
    Simeon Leyzerzon Guest

    Re: Extending DefaultHandler


    David,

    I changed 2 lines in your code and it works for me:

    ==> import org.apache.xerces.parsers.*;

    and in main

    ==>XMLReader xr = new SAXParser();

    Here is the full code:


    import org.apache.xerces.parsers.*;

    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import java.io.*;

    public class xmlEcho extends DefaultHandler
    {
    public void startDocument() throws SAXException
    {
    System.out.println("SAX Event: START Document");
    }

    public void endDocument() throws SAXException
    {
    System.out.println("SAX Event: END Document");
    }

    public void startElement(String namespaceURI,String localName, String qName,
    Attributes attrs) throws SAXException
    {
    System.out.println("SAX Event: START Element["
    +localName+"]");
    for (int i=0;i < attrs.getLength() ;i++ )
    {
    System.out.println(" ATTRIBUTE "+
    attrs.getLocalName(i)+" VALUE "+
    attrs.getValue(i));
    }
    }

    public void endElement(String namespaceURI,String localName,
    String qName) throws SAXException
    {
    System.out.println("SAX Event: END Element["
    +localName+"]");
    }

    public void characters(char ch[],int start, int length) throws SAXException
    {
    System.out.print("SAX Event: CHARACTERS[");
    try
    {
    OutputStreamWriter outw = new OutputStreamWriter(System.out);
    outw.write(ch,start,length);
    outw.flush();
    } catch (Exception e)
    {
    e.printStackTrace();
    }

    System.out.println("]");
    }

    public static void main(String args[])
    {
    System.out.println("Example1 SAX Events");
    try
    {
    //XMLReader xr = XMLReaderFactory.createXMLReader();
    XMLReader xr = new SAXParser();
    xr.setContentHandler(new xmlEcho());
    xr.parse(new InputSource(new FileReader("e:\\a.xml")));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    And here is the output:

    Example1 SAX Events
    SAX Event: START Document
    SAX Event: START Element[hello]
    SAX Event: CHARACTERS[
    ]
    SAX Event: START Element[goodbye]
    SAX Event: CHARACTERS[good-bye]
    SAX Event: END Element[goodbye]
    SAX Event: CHARACTERS[
    ]
    SAX Event: END Element[hello]
    SAX Event: END Document


    for the following xml rile (e:\a.xml):

    <hello>
    <goodbye>good-bye</goodbye>
    </hello>


    Hope it's helpful.

    Simeon








Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center