save Method (DOMDocument)

Switch View :
ScriptFree
 Collapse AllExpand All      Code: All Code: Multiple Code: Visual Basic Code: C# Code: C++ Code: F# Code: JScript 
save Method (DOMDocument)

Saves an XML document to the specified location.

Visual Basic
oXMLDOMDocument.save(destination);
Parameters

destination

An object. The object can represent a file name, an ASP Response object, a DOMDocument object, or a custom object that supports persistence. See Remarks for more information.

Visual Basic
oXMLDOMDocument.save(destination)
Example

The following Microsoft Visual Basic example creates a DOMDocument object from a string, then saves the document to a file in the application folder. If you look at the resulting file you will see that, instead of one continuous line of text, after each tag or data string. That is because of the vbNewLine constant inserted in the string at the appropriate locations.

Visual Basic
Dim doc As New DOMDocument30
   doc.async = False
   doc.validateOnParse = False
   doc.resolveExternals = False

   doc.loadXML _
      "<?xml version='1.0'?>" + vbNewLine + _
      "<doc title='test'>" + vbNewLine + _
      "   <page num='1'>" + vbNewLine + _
      "      <para title='Saved at last'>" + vbNewLine + _
      "          This XML data is finally saved." + vbNewLine + _
      "      </para>" + vbNewLine + _
      "   </page>" + vbNewLine + _
      "   <page num='2'>" + vbNewLine + _
      "      <para>" + vbNewLine + _
      "          This page is intentionally left blank." + vbNewLine + _
      "      </para>" + vbNewLine + _
      "   </page>" + vbNewLine + _
      "</doc>" + vbNewLine

   Path = App.Path + "\saved.xml"
   doc.save Path


Output

Outputs a file, saved.xml.

Example

If your DOMDocument contains unformatted XML, you might want to format it before saving. There is no flag in DOMDocument that can be set to indent your XML. In this situation, you might want to use the SAX writer to produce the resulting XML.

The following Microsoft Visual Basic example demonstrates how to perform DOM-to-SAX conversion to use the SAX writer's indent property. It then saves the output from the writer into a file in the application folder.

Visual Basic
'Create a DOMDocument object.
Dim xmlDoc As New DOMDocument60
'Create the SAX reader.
Dim rdr As New SAXXMLReader60
'Create the XML writer.
Dim wrt As New MXXMLWriter60
'File number
Dim iFileNo As Integer
iFileNo = FreeFile

'Load the DOM document.
xmlDoc.loadXML ("<doc><one>test1</one><two>test2</two></doc>")

'Set properties on the XML writer.
wrt.byteOrderMark = True
wrt.omitXMLDeclaration = True
wrt.indent = True

'Set the XML writer to the SAX content handler.
Set rdr.contentHandler = wrt
Set rdr.dtdHandler = wrt
Set rdr.errorHandler = wrt
rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt
'Parse the DOMDocument object.
rdr.parse xmlDoc

'Open the file for writing
Open App.Path + "\saved.xml" For Output As #iFileNo
Print #iFileNo, wrt.output
'Close the file
Close #iFileNo


Output

Outputs a file, saved.xml.

 
HRESULT save(
    VARIANT destination);
Parameters

destination[in]

The type of object to save. This object can represent a file name, an ASP Response object, an XML document object, or a custom object that supports persistence. See Remarks for more information.

C/C++ Return Values

S_OK

The value returned if successful.

XML_BAD_ENCODING

The value returned if the document contains a character that does not belong in the specified encoding. The character must use a numeric entity reference. For example, the Japanese Unicode character 20013 does not fit into the encoding Windows-1250 (the Central European alphabet) and therefore must be represented in markup as the numeric entity reference &#20013; or &#x4E2D; This version of save does not automatically convert characters to the numeric entity references.

E_INVALIDARG

The value returned if a string was provided, but it is not a valid file name.

E_ACCESSDENIED

The value returned if a save operation is not permitted.

E_OUTOFMEMORY

The value returned if the save operation must allocate buffers.

(Other values)

Any other file system error can be returned in the save(string) case.

Example

C++
BOOL DOMDocSaveLocation()
{
   BOOL bResult = FALSE;
   IXMLDOMDocument *pIXMLDOMDocument = NULL;
   HRESULT hr;

   try
   {
      _variant_t varString = _T("D:\\sample.xml");
      // Initialize pIXMLDOMDocument (create a DOMDocument).
      // Load document.
      hr = pIXMLDOMDocument->save(varString);
      if(SUCCEEDED(hr))
         bResult = TRUE;
   }
   catch(...)
   {
      DisplayErrorToUser();
   // Release the IXMLDOMDocument interface.
   }
   // Release the IXMLDOMDocument interface when finished with it.
   return bResult;
}


Remarks

The behavior differs based on the object specified by the objTarget parameter.

Object

Description

string

Specifies the file name. This must be a file name rather than a URL. The file is created, if necessary, and the contents are replaced entirely with the contents of the saved document. This mode is not intended for use from a secure client, such as Microsoft Internet Explorer.

VBS
dim xmldoc
    set xmldoc = Server.CreateObject("Msxml2.DOMDocument.3.0")
    xmldoc.load("c:\myfile.xml")
    xmldoc.save(Server.MapPath("sample.xml"))


The ASP Response object sends the document back to the client that invoked the ASP script.

VBS
dim xmldoc
    set xmldoc = Server.CreateObject("Msxml2.DOMDocument.3.0")
    xmldoc.load(Server.MapPath("sample.xml"))


IXMLDocument Object

Duplicates the original document. It is the equivalent of saving the document and reparsing it. The document goes through full persistence through XML markup, thereby testing the persistability of your XML document.

HTML
<script language="jscript">
        var xmldoc1 = new ActiveXObject("Msxml2.DOMDocument.3.0");
        var xmldoc2 = new ActiveXObject("Msxml2.DOMDocument.3.0");
        xmldoc1.load("sample.xml");
        xmldoc1.save(xmldoc2.XMLDocument);


Custom object supporting persistence

Any other custom COM object that supports QueryInterface for IStream, IPersistStream, or IPersistStreamInit can also 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; in the IPersistStream case, IPersistStreamLoad 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 point to the original document. A saved XML document might not load if the URLs are not accessible from the location in which you saved the document.

Character encoding is based on the encoding attribute in the XML declaration, such as <?xml version="1.0" encoding="windows-1252"?>. When no encoding attribute is specified, the default setting is UTF-8.

Validation is not performed during save, which can result in an invalid document that does not load again because of a specified document type definition (DTD).

This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).

Versioning

Implemented in: MSXML 3.0 and later

See Also

Concepts