very no

I just can't believe someone would design it this way on purpose. This is a one line task.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

Document doc = null;
try {
doc = docBuilder.parse (new File(fileName));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Seriously? DocumentBuilderFactory.newInstance()?

1 comment:

  1. This is the result of a pretty maximalist framework - you're supposed to be able to change the properties of your Factory and get a different sort of DocumentBuilder out, I believe.

    Obviously that means it is total overkill for the default case, here. The default Sun DOM parser is very heavyweight and kind of annoying. We made our own convenience class to handle it with a simpler interface. I hear good things about JDOM, though.

    Personally, the part that I can't believe is that all the catch blocks print a stack trace but continue execution with no other effect. This will even get you a NullPointerException later in the method if there was a ParserConfigurationException in the first try block.

    ReplyDelete