METHOD:  Document::save

This method is a Microsoft extension to the W3C DOM.

Document.save(objTarget)

The save method saves an XML document to a specified location.

There are four types of objects that can be specified as the parameter to this method, and each behaves slightly differently.

  • String
    In this case, the parameter specifies the file name. This must be a name and not a URL. If necessary, the file is created, or an existing file of the same name is overwritten. This mode is not intended for use from a secure client such as Internet Explorer.
  • ASP Response Object
    This mode sends the document back to the client that invoked the ASP script.
  • XML Document Object
    This mode duplicates the original document and is equivalent to saving and reparsing it. The document goes through full persistence through XML markup, thereby testing the persistability.
  • Custom Object Supporting Persistence
    Any custom COM object that supports QueryInterface for IStream, IPersistStream, or IPersistStreamInit can be provided here and the document will be saved accordingly. In the IStream case, the IStream Write method will be called as it saves the document, and in the IPersistStream case, IPersistStream Load will be called with an IStream that supports the Read, Seek, and Stat methods.

External entity references in DOCTYPE, ENTITY, NOTATION and xml namespace declarations are not changed; they continue to point to the original document meaning that a saved XML document might not load properly if the URLs are not accessible from its location.

Character encoding is based on the encoding attribute in the xml declaration. Where none is specified, the default setting is UTF-8.

Note that validation is not performed during a save, which can result in an invalid document not loading again because of the DTD.

In the first example we pass a string parameter. The 'albums.xml' file is loaded and the text of the last album's 'category' element is altered to 'Country'. This modified XML file is then saved in the Temp directory under the name of 'newAlbums.xml'.

XML:
<Albums>
   <Album ref="CD720">
      <category>Pop</category>
      <title>Come On Over</title>
      <artist>Shania Twain</artist>
   </Album>
   <Album ref="CD024">
      <category>Country & Western</category>
      <title>Red Dirt Girl</title>
      <artist>Emmylou Harris</artist>
   </Album>
</Albums>

Code (VBScript):
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("albums.xml")

Set NewText = objXMLDoc.createTextNode("Country")
Set Category = objXMLDoc.documentElement.lastChild.firstChild
Set OldText = Category.firstChild
Category.replaceChild NewText, OldText

objXMLDoc.save("C:\Temp\newAlbums.xml")

In this next example, we load the 'staff.xml' file and create a copy of it. To test that the operation has been successful, the code displays the XML of the copy.

XML:
<staff>
   <employee ssn="123456" pay="3">
      <name>John Sullivan</name>
      <position>&snrex;</position>
   </employee>
   <employee ssn="987654" pay="2">
      <name>Mary Lopez</name>
      <position>&pa;</position>
   </employee>
</staff>

Code (JavaScript):
xml_doc1 = new ActiveXObject("Microsoft.XMLDOM");
xml_doc2 = new ActiveXObject("Microsoft.XMLDOM");
xml_doc1.async = false;
xml_doc1.load("staff.xml");
xml_doc1.save(xml_doc2);

root = xml_doc2.documentElement;
document.write(root.xml);

Output:
John Sullivan &snrex; Mary Lopez &pa;



Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information