473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check XML for well-formedness using SAX

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!
Jul 20 '05 #1
2 6495
Urs Muntwyler wrote:
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.
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
Jul 20 '05 #2
A quick way to check for well-formedness of an xml document is to use
internet explorer.
"Urs Muntwyler" <um*@e-nvention.com> wrote in message
news:55**************************@posting.google.c om...
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!

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2741
by: Dave Smithz | last post by:
Hi there, A PHP application I built has a section which lists a number of members to a club whose names each appear with a check box beside them that can be ticked. These check boxes are part...
2
7152
by: Doug Baroter | last post by:
Hi, DDLs and DMLs: create table #job (jobID int identity(1,1) primary key, jobName varchar(25) unique not null, jobEndDate dateTime, jobComplete bit default(0), check (( is null and = 0) OR (...
3
9045
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called...
10
2379
by: Clive Backham | last post by:
I tried posting this on comp.infosystems.www.misc, but that group appears to get very little traffic. So now I'm trying here. If there is a more appropriate group, please let me know. I'm...
5
2021
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
7
29845
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
20
41863
by: anthonymelillo | last post by:
Is there any way to check the entry in a text box and make sure it is numeric ? Also, can I catch the enter key and make it call a button click event ? Sorry if this sounds stupid, but I am...
12
78694
by: Mokka | last post by:
i need a check that can go through a string to see if it contains any numbers. Please help!
1
4243
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
5
6705
by: Andrew Meador | last post by:
I have a form (Change Card List by Status) with a check box (cboNOT) and a list box (lstStatus). There is an Open Report button that opens a report (Report - Change Card List) which uses a query...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.