Check XML for well-formedness using SAX

U

Urs Muntwyler

Hi there

I have to check if the content of a file is a well-formed XML
document. Since the XML documents can be large, I'm using SAX to
perform this task.

Using Java, my code looks (somehow) like this:

public void checkForWellFormedness(File file)
{
SAXParser saxParser;
DefaultHandler dh;
// init parser
try {
SAXParserFactory spfactory = SAXParserFactory.newInstance();
saxParser = spfactory.newSAXParser();
dh = new DefaultHandler();
}
catch(Exception e) {
System.out.println("Cannot initialize SAX parser.");
e.printStackTrace();
}
// parse the XML document using SAX parser
try {
saxParser.parse(file,dh); // SAXException, IOException
}
catch(SAXException se) { // (*)
// only invoked in case of fatalError()
// what if error() occur? Is the XML document well-formed?
System.out.println("Document is not well-formed.");
se.printStackTrace();
}
catch(IOException ioe) {
System.out.println("Cannot read file.");
ioe.printStackTrace();
}
}


In the above code, you can see that I'm using the DefaultHandler
class, which implements the ErrorHandler interface. In this class, the
default implementation of fatalError() throws a SAXParseException,
which I'm catching in the above catch block (*). The default
implementation of error() does nothing, i.e. it does not throw a
SAXParseException. (I know what to do if I want that behaviour:
subclass DefaultHandler and overwrite the method).

My question is simple: what does it mean to have a well-formed XML
document? Does it mean to have no fatalError() occurring during
parsing (which means that the above code is ok), or is an XML document
well-formed if no fatalError() AND no error() occur? (which means that
I have to subclass DefaultHandler and overwrite method error() to
throw a SAXParseException so it is catched in the catch-block (*)).

Thanks for your help!
 
J

Johannes Koch

Urs said:
My question is simple: what does it mean to have a well-formed XML
document? Does it mean to have no fatalError() occurring during
parsing (which means that the above code is ok),

AFAIK well-formedness errors are fatal errors.
 
M

Martin SChukrazy

A quick way to check for well-formedness of an xml document is to use
internet explorer.
 

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,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top