Search code examples
javadomsax

Why DocumentBuilder.parse throws SaxParserException


The typical code snippet to create a DOM from an xml is as below:

Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("somefile.xml");

I was wondering why the parse method throws SAXException ?

I looked into the code and found that the DOMParser class parse(InputSource inputSource) method originally throws this exception up the chain.

Can anyone please help me understand, why DOM creation will need us to catch SaxException ? Isn't it kind of an oximoron?


Solution

  • Because it internally uses some SAX APIs.

    Note that this class reuses several classes from the SAX API. This does not require that the implementor of the underlying DOM implementation use a SAX parser to parse XML document into a Document. It merely requires that the implementation communicate with the application using these existing APIs.

    http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html