Click here to Skip to main content
13,558,908 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 21 Dec 2005

Writing XML using JavaScript

, 21 Dec 2005
Rate this:
Please Sign up or sign in to vote.
This article illustrates a Javascript XMLWriter object.


My most recent project makes heavy use of remote scripting (or what some people might call AJAX). As such, I wanted an easy way to write XML from JavaScript so that it could be sent to my server. Now, there might be a very simple way of doing this, but I don’t know it if there is. So I decided to write a JavaScript object to simplify the process of writing XML.

Using the code

The object is called XMLWriter. It automatically replaces invalid characters such as quotation marks or greater than signs with the appropriate XML values. However, it does not throw exceptions on invalid tag names, because the application I’m writing won’t have the possibility of producing invalid tag names. If you want to add tag-name validation to the object, it should not be a difficult task.

The XMLWriter object can be created with the JavaScript new command like so:

var XML = new XMLWriter();

The XMLWriter object has the following public methods:

  • BeginNode (Name)
  • EndNode ()
  • Attrib (Name, Value)
  • WriteString (Value)
  • Node (Name, Value)
  • Close ()
  • ToString ()

BeginNode writes out an opening tag, giving it the name that you pass the method. Below is an example, followed by the XML it would produce:

//Produces:  <Foo

EndNode ends the current node (if any are still open). So following from the BeginNode example, if we were to write XML.EndNode(), the writer would write “/>”. The object is smart enough to know if you have written any text or inner nodes out and will write “</Foo>” if necessary.

Attrib writes out an attribute and value on the currently open node. Below is an example, followed by the XML it would produce:

XML.Attrib("Bar", "Some Value");
//Produces: <Foo Bar="Some Value" />

WriteString writes out a string value to the XML document as illustrated below:

XML.WriteString("Hello World");
//Produces <Foo>Hello World</Foo>

The Node method writes out a named tag and value as illustrated below:

XML.Node("MyNode", "My Value");
//Produces: <MyNode>My Value</MyNode>

The Close method does not necessarily need to be called, but it’s a good idea to do so. What it does is end any nodes that have not been ended.

Finally, the ToString method returns the entire XML document as a single string (duh).


I’ve provided some sample code. The XMLWriter.js file contains all the code you will need to write XML. It is clean code, but uncommented. I’ve tested this code in IE 6.0 and FireFox 1.5.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer
United States United States
I'm a professional geek.

You may also be interested in...


Comments and Discussions

SuggestionAdding Indentation Pin
Member 1359956729-Dec-17 8:50
memberMember 1359956729-Dec-17 8:50 
QuestionXmlWriter Pin
DAVE_2314-Apr-17 20:59
memberDAVE_2314-Apr-17 20:59 
danazonas7-Nov-16 16:27
memberdanazonas7-Nov-16 16:27 
GeneralYou are the best.... Thanks a ton! Pin
Vinayaka Amaresh29-Oct-14 15:28
memberVinayaka Amaresh29-Oct-14 15:28 
QuestionCan you tell how to give the path of the xml file and write content to that xml. Pin
Member 828666113-Nov-13 1:15
memberMember 828666113-Nov-13 1:15 
Questionlink to xml file Pin
Member 1023844928-Aug-13 11:21
memberMember 1023844928-Aug-13 11:21 
Questionhow do i write something into the xml file.? Pin
Velrats6-Jun-13 5:59
memberVelrats6-Jun-13 5:59 
QuestionHow do I create the XML declaration? Pin
Samik Sengupta26-Feb-13 9:21
memberSamik Sengupta26-Feb-13 9:21 
QuestionNice work... but Pin
njfz12-Jun-12 4:16
membernjfz12-Jun-12 4:16 
Question[Object object] when using it with the $.ajax method Pin
Mortada.Issa22-Jun-11 7:29
memberMortada.Issa22-Jun-11 7:29 
AnswerRe: [Object object] when using it with the $.ajax method Pin
Mortada.Issa22-Jun-11 8:43
memberMortada.Issa22-Jun-11 8:43 
GeneralLicensing Pin
Ergin Salih22-Mar-11 5:13
memberErgin Salih22-Mar-11 5:13 
I am interested in using the XMLWriter code in a project we are creating here, to use this code I need to provide the license details. Your article does not specify a specific license agreement, can you update the article or let me know what license you would like to associate with this code.

GeneralRe: Licensing Pin
Ergin Salih22-Mar-11 7:08
memberErgin Salih22-Mar-11 7:08 
Generalsfs Pin
csz198318-Sep-10 3:32
membercsz198318-Sep-10 3:32 
GeneralMy vote of 1 Pin
harshitp18-Aug-10 22:42
memberharshitp18-Aug-10 22:42 
Generaluse your code in project Pin
Carsten_CRM1-Jan-10 7:04
memberCarsten_CRM1-Jan-10 7:04 
GeneralBeginNode doesn't work Pin
Gerinald30-Jul-09 15:05
memberGerinald30-Jul-09 15:05 
Generalvery useful Pin
nmreddy8321-Jun-09 8:28
membernmreddy8321-Jun-09 8:28 
QuestionUse in project Pin
manna17-Dec-08 3:16
membermanna17-Dec-08 3:16 
Generaluse in a project im working on Pin
tkahn611-Jun-08 17:06
membertkahn611-Jun-08 17:06 
Questionerror undifiend Pin
it_pushpakumara9-May-08 2:23
memberit_pushpakumara9-May-08 2:23 
GeneralQuestion about using in JSP page Pin
mastaboywonder27-Apr-08 12:23
membermastaboywonder27-Apr-08 12:23 
Generalsave file Pin
nh3simman17-Apr-07 18:15
membernh3simman17-Apr-07 18:15 
GeneralRe: save file Pin
Robertfi26-Jan-11 0:27
memberRobertfi26-Jan-11 0:27 
GeneralWant to write existing xml file Pin
pravin parmar7-Dec-06 0:37
memberpravin parmar7-Dec-06 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by christophilus
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid