Loading and Saving XML

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Loading and Saving XML

Microsoft® XML Core Services (MSXML) 5.0 for Microsoft Office provides two methods for loading XML documents into the Document Object Model (DOM) and one method and one property for serializing that object model back into an XML stream. The load and loadXML methods parse documents into a DOM. The save method and xml property convert the DOM back into an XML document. The load and save methods are used to retrieve information from outside a script, while the loadXML method and xml property are generally used inside scripts, parsing and returning XML strings found inside.

Use the load method to load an XML file by path name, URL, or from a Microsoft Internet Information Services (IIS) Request object.

The following example loads an XML file by URL.

XMLDoc.load("http://www.examples.microsof.com/reports.xml")

The document object, XMLDoc, will load the reports.xml document, parse it, and, depending on the content of the document, create an object tree representing the document or report an error.

By default, the loading and parsing of an XML file occurs asynchronously. To load the XML file synchronously, set the async property to False. This forces the loading of the entire XML document before processing continues. To abort an asynchronous download, call the abort method on the document object.

You can also load an XML file as a string using the loadXML method.

XMLDoc.loadXML("<customer><first_name>Joe</first_name>
  <last_name>Smith</last_name></customer>")

To save a parsed XML document to a file or another object, use the save method. The save method takes either a file name as a string, an Active Server Pages (ASP) Response object, a DOMDocument object, or any custom object that supports persistence. The following Microsoft Visual Basic® Scripting Edition (VBScript) example is part of an ASP page that saves the XML document to a file.

<%
  dim xmldoc
  set xmldoc = Server.CreateObject("Msxml2.DOMDocument.5.0")
  xmldoc.async = false
  xmldoc.load(Request)
  xmldoc.save(Server.MapPath("sample.xml"))
%>

The following example is in Microsoft JScript®.

<%
  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
  xmldoc.async = false;
  xmldoc.load(Request);
  xmldoc.save(Server.MapPath("sample.xml"));
%>

You can use the xml property of DOMDocument to retrieve the XML document as a string. This allows you to retrieve a Unicode string representation of the XML document model. An ASP script that uses the XML parser to process information sent in the Request object is shown in VBScript.

<%
  dim xmldoc
  set xmldoc = Server.CreateObject("Msxml2.DOMDocument.5.0")
  xmldoc.async = false
  xmldoc.load(Request)
  xmlAsString=xmldoc.xml
%>

The following example is in JScript.

<%
  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
  xmldoc.async = false;
  xmldoc.load(Request);
  xmlAsString=xmldoc.xml;
%>

See Also

async Property | load Method | loadXML Method | save Method