XML DOM » Document » save

Compability: Internet Explorer

Syntax:
document.save(objTarget)

This 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.


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.

Examples

Code:
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>

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")
Explanation:

In this 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'.

Language(s): VBScript XML
Code:
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>

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;
Explanation:

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.

Language(s): JavaScript XML