SAX and DOM query

G

gk

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.IOException;


public class DOMExample{

public static void main(String args[]) throws IOException,
SAXException{

DOMParser parser = new DOMParser();
parser.parse("games.xml");

Document dom = parser.getDocument();
}

}

In the body of the main method, you have the code that creates a new
instance of the DOMParser class and assigns it to the variable named
parser. The second line calls the parse method of the DOMParser class
and passes in the name of the XML file you have created.

The parse method throws two exceptions: java.io.IOException and
org.xml.sax.SAXException. Rather than wrap the call to the parse method
in a try/catch, main throws these two exceptions.


Question:
 
W

Wesley Hall

Question:
----------------

its very surprising to me that there is no DOMException! ,.....look,
we are studying here DOM implementation .... its the DOM chapter
then why SAXException came ?

It is because the DOMParser actually uses SAX to build the DOM
representation of the XML. If you were to look inside the DOMParser code
you would see lots of SAX code used to parse the XML and store it inside
the DOM classes.

You could argue that the API should catch the SAXException and wrap it
in a DOM specific exception before throwing, you may have a point, it is
probably just more hassle than it is worth :)
 
S

Simon Brooke

gk said:
import org.apache.xerces.parsers.DOMParser;

Shouldn't do that. That's a bad mistake which I made back in 2000, which
came back and bit me very hard two years later. Importing a particular DOM
parser means you can use methods with are not part of the DOM interface
but are peculiar to that parser, which means when you use another parser
things will break horribly.

You want to use interfaces out of org.w3c.dom and javax.xml.parsers only.
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.IOException;


public class DOMExample{

public static void main(String args[]) throws IOException,
SAXException{

DOMParser parser = new DOMParser();

Shouldn't to that. Do

DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance( );

DocumentBuilder parser = null;

try
{
parser = dbf.newDocumentBuilder( );
}
catch ( ParserConfigurationException pce )
{
throw new
GenerationException( "Could not create XML parser", pce );
}
parser.parse("games.xml");

Don't do that, that method isn't part of the published API. use e.g.


Document dom =
parser.parse(
new InputSource(
new URL( xslDocumentName ).openStream( ) ) );
Document dom = parser.getDocument();
}

}

In the body of the main method, you have the code that creates a new
instance of the DOMParser class and assigns it to the variable named
parser. The second line calls the parse method of the DOMParser class
and passes in the name of the XML file you have created.

The parse method throws two exceptions: java.io.IOException and
org.xml.sax.SAXException. Rather than wrap the call to the parse method
in a try/catch, main throws these two exceptions.

You shouldn't be using that parse method, is pretty much the answer. What
org.apache.xerces.parsers.DOMParser does internally is none of your
business.
Question:
----------------

its very surprising to me that there is no DOMException! ,.....look,
we are studying here DOM implementation .... its the DOM chapter
then why SAXException came ?

Because that's what the API specification says they can throw. You can
build a DOM parser on top of a SAX parser, and it looks from the
documentation as if that's what you're expected to do. For bonus points,
why can you not build a SAX parser on top of a DOM parser?
 
S

Simon Brooke

Wesley said:
It is because the DOMParser actually uses SAX to build the DOM
representation of the XML. If you were to look inside the DOMParser code
you would see lots of SAX code used to parse the XML and store it inside
the DOM classes.

You could argue that the API should catch the SAXException and wrap it
in a DOM specific exception before throwing, you may have a point, it is
probably just more hassle than it is worth :)

Actually, if you built a DOM parser without a SAX parser under it, you'd
still be required to throw SAXExceptions, because that's what the DOM API
specifies.
 
W

Wesley Hall

Simon said:
Actually, if you built a DOM parser without a SAX parser under it, you'd
still be required to throw SAXExceptions, because that's what the DOM API
specifies.

True. Which probably lends a degree of credence to the OPs assertion
that it poor design.
 
G

gk

Simon said:
gk said:
import org.apache.xerces.parsers.DOMParser;

Shouldn't do that. That's a bad mistake which I made back in 2000, which
came back and bit me very hard two years later. Importing a particular DOM
parser means you can use methods with are not part of the DOM interface
but are peculiar to that parser, which means when you use another parser
things will break horribly.

You want to use interfaces out of org.w3c.dom and javax.xml.parsers only.
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.IOException;


public class DOMExample{

public static void main(String args[]) throws IOException,
SAXException{

DOMParser parser = new DOMParser();

Shouldn't to that. Do

DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance( );

DocumentBuilder parser = null;

try
{
parser = dbf.newDocumentBuilder( );
}
catch ( ParserConfigurationException pce )
{
throw new
GenerationException( "Could not create XML parser", pce );
}
parser.parse("games.xml");

Don't do that, that method isn't part of the published API. use e.g.


Document dom =
parser.parse(
new InputSource(
new URL( xslDocumentName ).openStream( ) ) );
Document dom = parser.getDocument();
}

}

In the body of the main method, you have the code that creates a new
instance of the DOMParser class and assigns it to the variable named
parser. The second line calls the parse method of the DOMParser class
and passes in the name of the XML file you have created.

The parse method throws two exceptions: java.io.IOException and
org.xml.sax.SAXException. Rather than wrap the call to the parse method
in a try/catch, main throws these two exceptions.

You shouldn't be using that parse method, is pretty much the answer. What
org.apache.xerces.parsers.DOMParser does internally is none of your
business.
Question:
----------------

its very surprising to me that there is no DOMException! ,.....look,
we are studying here DOM implementation .... its the DOM chapter
then why SAXException came ?

Because that's what the API specification says they can throw. You can
build a DOM parser on top of a SAX parser, and it looks from the
documentation as if that's what you're expected to do. For bonus points,
why can you not build a SAX parser on top of a DOM parser?

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
; ... of course nothing said here will be taken notice of by
; the W3C. The official place to be ignored is on www-style or
; www-html. -- George Lund




you mentioned couple of good points .
I am very upset....... why the books does not teach correct way !!
the code i have posted is from the book ,

"Java, XML, and Web Services Bible by Mike Jasnowski et al."

It seems , the time has come to throw away this book ......its
completely pushing me into a bad design and wrong way.

I am learning and i want to get the best desgin . I will appreciate if
you suggest couple of good books .
 
G

gk

Wesley said:
It is because the DOMParser actually uses SAX to build the DOM
representation of the XML. If you were to look inside the DOMParser code
you would see lots of SAX code used to parse the XML and store it inside
the DOM classes.

You could argue that the API should catch the SAXException and wrap it
in a DOM specific exception before throwing, you may have a point, it is
probably just more hassle than it is worth :)

Thank you . this is very nice .
 
M

Mike Schilling

gk said:
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.IOException;


public class DOMExample{

public static void main(String args[]) throws IOException,
SAXException{

DOMParser parser = new DOMParser();
parser.parse("games.xml");

Document dom = parser.getDocument();
}

}

In the body of the main method, you have the code that creates a new
instance of the DOMParser class and assigns it to the variable named
parser. The second line calls the parse method of the DOMParser class
and passes in the name of the XML file you have created.

The parse method throws two exceptions: java.io.IOException and
org.xml.sax.SAXException. Rather than wrap the call to the parse method
in a try/catch, main throws these two exceptions.


Question:
----------------

its very surprising to me that there is no DOMException! ,.....look,
we are studying here DOM implementation .... its the DOM chapter
then why SAXException came ?

Bad naming conventions. There is a class DOMException, but t's thrown when
manipulating a DOM illegally, e.g.

Element elm;
elm.appendChild(elm);

Parsing an XML file can't result in that sort of error, so DOMException
won't be thrown. Both DOM and SAX parsers throw the same sort of exceptions
when parsing XML, and they should probably be called XMLParserException
instead of SAXException.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top