Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2003, 12:26 PM
sax sax is offline
Registered User
 
Join Date: Nov 2003
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");
        }
}
------------------------------------





 
Old November 17th, 2003, 08:35 AM
Friend of Wrox
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.