Click here to Skip to main content
Licence 
First Posted 21 Dec 2005
Views 132,860
Downloads 2,211
Bookmarked 53 times

Writing XML using JavaScript

By christophilus | 21 Dec 2005
This article illustrates a Javascript XMLWriter object.
3 votes, 20.0%
1

2

3
2 votes, 13.3%
4
10 votes, 66.7%
5
4.86/5 - 15 votes
3 removed
μ 4.00, σa 2.84 [?]

Introduction

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:

XML.BeginNode(“Foo”);
//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.BeginNode(“Foo”);
XML.Attrib(“Bar”, “Some Value”);
XML.EndNode();
//Produces: <Foo Bar=”Some Value” />

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

XML.BeginNode(“Foo”);
XML.WriteString(“Hello World”);
XML.EndNode();
//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).

Summary

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.

License

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

christophilus

Software Developer

United States United States

Member
I'm a professional geek.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Question[Object object] when using it with the $.ajax method PinmemberMortada.Issa8:29 22 Jun '11  
AnswerRe: [Object object] when using it with the $.ajax method PinmemberMortada.Issa9:43 22 Jun '11  
GeneralLicensing PinmemberErgin Salih6:13 22 Mar '11  
GeneralRe: Licensing PinmemberErgin Salih8:08 22 Mar '11  
Generalsfs Pinmembercsz19834:32 18 Sep '10  
GeneralMy vote of 1 Pinmemberharshitp23:42 18 Aug '10  
Generaluse your code in project PinmemberCarsten_CRM8:04 1 Jan '10  
GeneralBeginNode doesn't work PinmemberGerinald16:05 30 Jul '09  
Generalvery useful Pinmembernmreddy839:28 21 Jun '09  
QuestionUse in project Pinmembermanna4:16 17 Dec '08  
Generaluse in a project im working on Pinmembertkahn618:06 11 Jun '08  
Questionerror undifiend Pinmemberit_pushpakumara3:23 9 May '08  
GeneralQuestion about using in JSP page Pinmembermastaboywonder13:23 27 Apr '08  
Generalsave file Pinmembernh3simman19:15 17 Apr '07  
GeneralRe: save file PinmemberRobertfi1:27 26 Jan '11  
GeneralWant to write existing xml file Pinmemberpravin parmar1:37 7 Dec '06  
GeneralRe: Want to write existing xml file Pinmemberdesigning_designing4:00 18 Sep '08  
i m searching for the same despretly if you get it plz send me at vikassharma@mansainfotech.com
 
thanks..
GeneralCode suggestion PinmemberMike On Code4:11 20 May '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120210.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by christophilus
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid