How to Create an XML File With Javascript
XML has come to be regarded as an important base on which Web Services work. Businesses can work together upon agreed standards for exchanging data that are all built on XML. In addition, XML does not need to be decoded in any way or require other data solutions and does not need special expertise. Being the new standard language for data formatting over the Internet, it works seamlessly with JavaScript, another web scripting language.
Instructions
-
File Creation
-
1
Create a JavaScript object using the “new” command as shown below:<br /><br />var XML = new XMLWriter()<br />GO<br /><br />Use the “XMLWriter” object to initiate public methods (properties associated with the XML element).
-
2
Add the encoding and version arguments to the constructor as shown below:<br /><br />Var XML = new XMLWriter ( ‘UTF-8’, 1.0’);
-
-
3
Write an opening tag. This creates a name for the first element together with the markup in XML format as shown below:<br /><br />XML.BeginNode (“Foo”)<br />GO<br /><br />The above code produces the line:<br /><br /><Foo
-
4
Write the closing tag. Use the “EndNote()” method to write the closing tag and thus complete the first XML element as code below shows:<br /><br />XML.BeginNode(“Foo”)<br />GO<br />XML.EndNode()<br />GO<br /><br />This produces the line:<br /><br /><Foo><br /></Foo>
-
5
Add an attribute to the element using the method “WriteString()” as the code below displays:<br /><br />XML.BeginNode (“Foo”)<br />GO<br />XML.WriteString(“Hello World”)<br />GO<br />XML.EndNote()<br />GO<br /><br />These lines of code produce the line below:<br /><br /><Foo> Hello World</Foo>
-
6
Write a named tag and add a value as illustrated below:<br /><br />XML.BeginNode(“Foo”)<br />GO<br />XML.EndNode()<br />GO<br />XML.Node(“MyNode”, “My Value”)<br />GO<br /><br />The above line of code writes the XML element and value below:<br /><br /><Foo> Hello World</Foo><br /><MyNode>My Value</MyNode>
-
7
Build the entire XML document by using the syntax:<br /><br />// This writes the entire XML document together with the attributes<br />function element(name,content, attributes){<br />// XML writer with attributes and smart attribute quote escaping <br />function element(name,content,attributes){<br /> var att_str = ''<br /> if (attributes) { // tests false if this arg is missing!<br /> att_str = formatAttributes(attributes)<br /> }<br /> var xml<br /> if (!content){<br /> xml='<' + name + att_str + '/>'<br /> }<br /> else {<br /> xml='<' + name + att_str + '>' + content + '</'+name+'>'<br /> }<br /> return xml<br />}
-
1
Tips & Warnings
The encoding and version arguments added to the constructor are optional arguments. However, they help avoid cross-browser incompatibilities in certain browsers.<br /><br />The “Close()” tag does not necessarily need to be added. However, it is good practice to add it to end any nodes that have not been ended.<br /><br />When testing the above code, use the element below to display the results for your codeL:<br /><br />Alert(XML);