Creating an XML file using JavaScript

Source: Internet
Author: User
Tags install window object model requires client
javascript|xml| Create

You can use the FileSystemObject (FSO) object or use the XML Document Object Model (DOM) directly on the client to create an XML file. If the FSO is used, the client must install Window Script 5.5, and using the XML DOM requires Microsoft®xml Core Services to be installed on the client. Security requires that ActiveX controls be enabled in the security settings of IE.

1, use the FSO to create an XML file

Use the GetSpecialFolder method of the FSO object to obtain the directory where the file is to be created (GetSpecialFolder the settings for the method parameter see the Window Scripting Technical documentation), Then use the CreateTextFile method to create a text file and get an object reference to the file, invoke the WriteLine method of the text file object to write the content to the file, and finally close the object.

It is important to note that when you create a file using CreateTextFile, the last parameter that indicates how the file was created should be set to true to indicate the creation in Unicode and the encoding of the XML file to UTF-16 so that the XML file created can be used correctly.

<script language=javascript>

function Createxml () {

var fso, TempFolder, xmlfile, Schar;

FSO = new ActiveXObject ("Scripting.FileSystemObject");

TempFolder = fso. GetSpecialFolder (0);

XMLFile = fso. CreateTextFile (tempfolder+) XML. XML ", true,true);

Schar = ' \ r ';

XMLFile. WriteLine (' <?xml version= "1.0" encoding= "UTF-16"?> ' +schar ");

XMLFile. WriteLine (' <document title= ' information ' > ' +schar);

for (Var i=0;i<10;i++) {

XMLFile. WriteLine (' <xmlnode type= "node" name= "node ' +i+ '"/> ' +schar ');

}

XMLFile. WriteLine (' </document> ');

XMLFile. Close ();

}

</script>

2. Creating XML files using XML DOM

First creates a DOMDocument object, calls its Load method to load a local file, creates a new file if the file does not exist during the loading process, constructs the document content string, calls the Loadxml method to load the XML content after construction, and finally calls the Save method to write the file.

Creating an XML file using the XML DOM requires careful content formatting, otherwise there will be no content in the file created, because XmlDoc has already judged the format when writing the file, and the Save method fails if it is incorrect.

<script language=javascript>

function Createxml () {

var xmldoc, XmlNode;

xmldoc = new ActiveXObject ("msxml2.domdocument.4.0");

Xmldoc.load ("C:\\xml.xml");

Schar = ' \ r ';

Xmlhead = ' <?xml version= ' 1.0 ' encoding= ' UTF-16 '?> ' +schar;

Xmltitle = ' <documenttitle= ' information ' > ' +schar;

XmlNode = ';

for (Var i=0;i<10;i++) {

XmlNode = xmlnode + ' <xmlnode type= "node" name= "node ' +i+ '"/> ' +schar;

}

Xmlfoot = ' </document> ';

Strxml = Xmlhead+xmltitle+xmlnode+xmlfoot;

Xmldoc.loadxml (strxml);

Xmldoc.save ("C:\\xml.xml");

}

</script>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.