Opening, Closing, and Saving documents
DOM Level 2 does not provide methods to open, save, and close documents. However, the AOM includes methods on the Application and ADocument interfaces that implement these capabilities.
The Application interface openDocument method returns a Document object that has information about a document or document type and can be used to dynamically update the content, structure, and style of the document
The
openDocument method takes several optional parameters, including the
flags parameter, which controls the state in which the document is opened. This parameter is constructed by adding the hex values of the
LoadFlag enumeration constants. (The symbolic constant names can be used instead with some language bindings.) Refer to
Application interface for a complete listing and full descriptions of the
LoadFlag enumeration constants. The following table highlights a selection of these constants.
Name
|
Hexadecimal value
|
Description
|
OPEN_RDONLY
|
0x0001
|
Open the document as read only.
|
OPEN_DOCRDWR
|
0x0002
|
Open the document for read and write.
|
OPEN_NOMSGS
|
0x0020
|
Suppress any parser error messages.
|
OPEN_EDITINIT
|
0x8000
|
Process initialization files upon opening.
|
In the following code, the flags parameter is used to open a document for read and write while suppressing any parser errors:
var doc = Application.openDocument("mydocument.xml", (0x0002 + 0x0020))
Once a document is opened, it can be manipulated and then saved and closed using methods of the ADocument interface (which extends the W3C DOM Document interface).
ADocument.save writes the document to disk. The save method's flags parameter determines the state of the saved document.
ADocument.close frees all resources associated with the Document object.
Refer to the examples in the remainder of this chapter for several sample uses of the Application.openDocument, ADocument.save, and ADocument.close methods.