Java Reference
In-Depth Information
"DOM Parser Error",
JOptionPane.ERROR_MESSAGE);
}
// Handles fatal errors from parsing XML
public void fatalError(SAXParseException spe) throws SAXParseException {
JOptionPane.showMessageDialog(SketcherFrame.this,
"Fatal error at line " + spe.getLineNumber() + "\n" +
spe.getMessage(),
"DOM Parser Error",
JOptionPane.ERROR_MESSAGE);
throw spe;
}
// Handles warnings from parsing XML
public void warning(SAXParseException spe) {
JOptionPane.showMessageDialog(SketcherFrame.this,
Warning at line " + spe.getLineNumber() + "\n" +
spe.getMessage(),
"DOM Parser Error",
JOptionPane.ERROR_MESSAGE);
}
Directory "Sketcher reading and writing XML"
In each case you display a dialog providing information about the error. For a fatal error, you throw
SAXParseException , which terminates parsing. There should not be any errors occurring because you are
processing XML that was created by the code in Sketcher. You need another import statement in Sketcher-
Frame.java :
import org.xml.sax.*;
The SketcherFrame object can now create a DOM Document object encapsulating the entire sketch. All
you now need is some code to provide the GUI for exporting sketches as XML, and code to use the Docu-
ment object to write an XML file.
Saving a Sketch as XML
Of course, you could modify Sketcher so that you could set an option to save sketches either as objects or
as XML documents. However, to keep things simple you will add menu items to the File menu to export or
import a sketch as XML. In this way, you keep the code for reading and writing XML files separate from
the code for reading and writing .ske files.
In broad terms, here's what you have to do to the SketcherFrame class to save a sketch as an XML file:
• Add Import XML and Export XML menu items.
• Implement the process of creating an XML document from the Document object that the cre-
ateDocument() method creates in response to an Export XML menu item event.
• Implement writing the XML document that is generated from a sketch to a file.
You can add new FileAction objects for the two new menu items. Clearly, a lot of the work is in the im-
plementation of the new functionality in the actionPerformed() method in the FileAction class, so let's
Search WWH ::




Custom Search