Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 10th, 2003, 12:26 PM
sax sax is offline
Registered User
 
Join Date: Nov 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default import org.xml.sax is deprecated

Hi All, Please help ... I am a beginner of using SAX with Java. I think I don't have the org.xml.sax.* classes in my java class. So, I downloaded "saxjava-1.0" from www.megginson.com (I hope I downloaded the correct one). I unzipped the file and put it in my Java folder. However, it doesn't work when I run my Java code. I think I might missed out something ... like didn't import the file to Java classes? If so, how can I do that? My java code is trying to read an XML file and print out how many books in the xml file. After compiled, it has the following message:

"package com.jclark.xml.sax does not exists"

and 2 compiler warnings:
C:\XML\BookCounter.java:12:warning:org.xml.sax.Han dlerBase in org.xml.sax has been deprecated.

C:\XML\BookCounter.java:21:warning:org.xml.sax.Par ser in org.xml.sax has been deprecated.

My java code as follow:
------------------------------
import org.xml.sax.*;


public class BookCounter extends HandlerBase
{
    public static void main (String args[]) throws Exception
    {
           (new BookCounter()).countBooks();
    }

        public void countBooks() throws Exception
        {
            Parser p = new com.jclark.xml.sax.Driver();
            p.setDocumentHandler(this);
            p.parse("file:///C:/books.xml");
        }
}
------------------------------------





Reply With Quote
  #2 (permalink)  
Old November 17th, 2003, 08:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

it'll be better to separately define a Handler class.
Code:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SimpleHandler extends DefaultHandler
{
  public static int bookCount = 0;

  public void startElement(String namespace, String localName, String qName, Attributes attributes) throws SAXException
  {
    if(localName.equals("book"))  {
      ++bookCount;
    }
  }
}
Then you can test it like this:
Code:
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.ParserAdapter;
import java.io.IOException;
import org.xml.sax.SAXException;

public class TestSAX {
  public static void main(String[] args)
  {
    XMLReader reader = null;
    if(System.getProperty("org.xml.sax.parser") == null) {
      System.setProperty("org.xml.sax.parser",  "org.apache.xerces.parsers.SAXParser");
    }
    try {
      reader = new ParserAdapter();
      reader.setContentHandler(new SimpleHandler());
      reader.parse("books.xml");
      System.out.println("C = " + SimpleHandler.bookCount);
    } catch (SAXException ex) {
      ex.printStackTrace();
    } catch(IOException ioe) {
      ioe.printStackTrace();
    }
  }
}
I've set Xerces as the default SAX parser, although you can set your preferred SAX parser through JVM parameter.

Regards,
Armen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
org.xml.sax.SAXParseException: Premature end ksskumar XSLT 1 March 19th, 2008 04:01 AM
How to parse xml using SAX Parser?? priyatowin XSLT 2 September 8th, 2006 02:56 AM
package org.xml does not exist?? CFerthorney J2EE 1 May 10th, 2004 05:26 PM
java.lang.NoClassDefFoundError: org/xml/sax/XMLRea kcheung XSLT 0 August 8th, 2003 08:03 AM
GenericDataSource Deprecated lizethfc JSP Basics 0 June 5th, 2003 11:08 AM



All times are GMT -4. The time now is 05:32 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.