• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I see alot of JAXP references on JR

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me if JAXP is being used successfully to process XML data using the SAX model please?

I've been trying to use DOM4J, but i'm having some problems capturing the attribute data and associating it with the element data, so i'm searching for a more intuitive approach!

But maybe an even bigger question, after browsing the last 100 or so XML posts, is that so far, i've been testing various XML tools on Windows, but in the production world, i will be using the Sun Solaris box, so should i be using JAXP anyway?

Thanks much!
bc

:roll:
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JAXP is just a (small) set of classes and interfaces with which you decouple your application code from a specific parser implementation (i.e. you instantiate instances of javax.xml.* instead of, say, org.apache.*). Yes, JAXP is used successfully for all sorts of XML processing, including SAX and DOM. In fact, I would go as far as saying that JAXP is the dominant API used for XML processing in Java applications written today.
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Lasse!

Can i trouble you for another question please?

I copied an example of JAXP(SAX2) code from the OREILLY web site and it uses the 'xerces.jar' file in the code, specifically "org.apache.xerces.parsers.SAXParser();

I downloaded the closest thing to the xerces name specified, under the Xerces-J website, the book specifies using 'xerces.jar', but i only could find 'xercesImpl.jar' !

So i set the classpath to where i unzipped it and also added a copy of the the .jar file to the \lib directory of the j2sdk directory, out of desparation!

set CLASSPATH=c:\j2sdk1.4.2_03\lib;c:\java\hlp\jaxp;C:\UNZIPPED DOWNLOADS\XERCES\xercesImpl.jar;

So when i compiled it:

C:\JAVA\HLP\JAXP>javac ListServlets2.java

I got the following error:

ListServlets2.java:19: package org.apache.xerces.parsers does not exist
org.xml.sax.XMLReader parser=new org.apache.xerces.parsers.SAXParser();

Lasse, i'm wondering if i need to specify a package name in the code or is the import statement the correct approach when bringing in an org. file?

Here is the code!

import org.xml.sax.*; // The main SAX package
import org.xml.sax.helpers.*; // SAX helper classes
import java.io.*; // For reading the input file
import java.util.*; // Hashtable, lists, and so on

/**
* Parse a web.xml file using the SAX2 API and the Xerces parser from the
* Apache project.
*
* This class extends DefaultHandler so that instances can serve as SAX2
* event handlers, and can be notified by the parser of parsing events.
* We simply override the methods that receive events we're interested in
**/
public class ListServlets2 extends org.xml.sax.helpers.DefaultHandler {
/** The main method sets things up for parsing */
public static void main(String[] args) throws IOException, SAXException {
// Create the parser we'll use. The parser implementation is a
// Xerces class, but we use it only through the SAX XMLReader API
org.xml.sax.XMLReader parser=new org.apache.xerces.parsers.SAXParser();<<<<<<<<<<<<<

// Specify that we don't want validation. This is the SAX2
// API for requesting parser features. Note the use of a
// globally unique URL as the feature name. Non-validation is
// actually the default, so this line isn't really necessary.
parser.setFeature("http://xml.org/sax/features/validation", false);

// Instantiate this class to provide handlers for the parser and
// tell the parser about the handlers
ListServlets2 handler = new ListServlets2();
parser.setContentHandler(handler);
parser.setErrorHandler(handler);

// Create an input source that describes the file to parse.
// Then tell the parser to parse input from that source
org.xml.sax.InputSource input=new InputSource(new FileReader(args[0]));
parser.parse(input);
}

.....................

Thanks again Lasse!
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it Lasse!

I not exactly sure why it didn't work the first time, but i put a copy of the "jsdk\lib\xercesLib.jar" statement at the end of the classpath and it worked! even though i had it correctly referenced in the 'unzipped' file statement!

I quess some things never quite make sense, but we do it anyway!

Thanks again! Lasse and thanks for all your help in the past too!

 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bob connolly:
I copied an example of JAXP(SAX2) code from the OREILLY web site and it uses the 'xerces.jar' file in the code, specifically "org.apache.xerces.parsers.SAXParser();

I don't know why they import the Xerces class directly.

Originally posted by bob connolly:
I downloaded the closest thing to the xerces name specified, under the Xerces-J website, the book specifies using 'xerces.jar', but i only could find 'xercesImpl.jar' !

That's the one. They renamed the file at some point.

In your code example

a "pure" JAXP version would be to use the javax.xml.parsers.* package like this (note that I'm not referring to a single Xerces-specific class):
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmm, interesting, makes it alot simpler for sure using the default!

Thanks again Lasse, sure appreciate you guys at the ranch!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic