Java Reference
In-Depth Information
JOptionPane.ERROR _ MESSAGE);
}
public void error(org.xml.sax.SAXParseException spe) {
JOptionPane.showMessageDialog(SketchFrame.this,
"Error at line "+spe.getLineNumber()
+ "\n"+spe.getMessage(),
"DOM Parser Error",
JOptionPane.ERROR _ MESSAGE);
}
}
This implements the three methods declared in the ErrorHandler interface. In contrast to our
previous example using a DOM error handler, rather than writing error information to the command
line, here we display it in a suitable dialog.
Here's how we can implement the openXMLSketch() method in the SketchFrame class:
private void openXMLSketch(File xmlFile) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setValidating(true); // Add validating parser feature
builderFactory.setIgnoringElementContentWhitespace(true);
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder.setErrorHandler(new DOMErrorHandler());
checkForSave();
theApp.insertModel(createSketchModel(builder.parse(xmlFile)));
filename = xmlFile.getName(); // Update the file name
setTitle(frameTitle+xmlFile.getPath()); // Change the window title
sketchChanged = false; // Status is unchanged
} catch(ParserConfigurationException e) {
JOptionPane.showMessageDialog(SketchFrame.this,
e.getMessage(),
"DOM Parser Factory Error",
JOptionPane.ERROR _ MESSAGE);
e.printStackTrace(System.err);
} catch(org.xml.sax.SAXException e) {
JOptionPane.showMessageDialog(SketchFrame.this,
e.getMessage(),
"DOM Parser Error",
JOptionPane.ERROR _ MESSAGE);
e.printStackTrace(System.err);
} catch(IOException e) {
JOptionPane.showMessageDialog(SketchFrame.this,
e.getMessage(),
"I/O Error",
Search WWH ::




Custom Search