• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

validating xml problem

Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to read an xml data with validating the input. But the
validation did not seem to do its job. Can anybody help to point
out what is problem? Your help is appreciated.
The following are codes and xml data:

When I run the program and it did not complain on wrong tagName.
Anything wrong in above codes?
-- Added UBB CODE tags to aid code readability
[This message has been edited by Frank Carver (edited September 29, 2000).]
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you may be aware, in Sun implementation of JAXP, SAX parser is being used to build DOM object. if you don't handle error conditions, it may not do anything.
I have added
docBuilder.setErrorHandler(new MyErrorHandler());
in your program.
Also I have added a class MyErrorHandler which extends HandlerBase to handler error.
Now, it works. You may checkup.
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.*;
public class Xml1 {
public static void main (String argv []) {
Document doc= null;
if (argv.length != 1) {
System.err.println ("Usage: cmd filename");
System.exit (1);
try {
DocumentBuilderFactory factory =
DocumentBuilder docBuilder =
//************** Added to handle error
docBuilder.setErrorHandler(new MyErrorHandler());
doc = docBuilder.parse (new File (argv [0]));
doc.getDocumentElement ().normalize ();
} catch (SAXParseException err) {
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
System.out.println ("error");
} catch (Throwable t) {
t.printStackTrace ();
System.out.println ("error");
System.exit (0);
class MyErrorHandler extends HandlerBase {
public void error (SAXParseException e) throws SAXParseException {
throw e;

jeff zhang
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working now after ErrorHandeler configured.
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question probably, but...
Can someone explain why the following is needed...

even though the entire parsing processing code is within try/catch clauses which catch the same error as the MyErrorHandler class? (as follows)

Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!