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�);
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();
WriteString writes out a string value to the XML document as illustrated below:
XML.BeginNode(�Foo�);
XML.WriteString(�Hello World�);
XML.EndNode();
The Node method writes out a named tag and value as illustrated below:
XML.Node(�MyNode�, �My Value�);
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.
| You must Sign In to use this message board. |
|
|
 |
|
 |
My js code is:
function writeInit(label,link) { var initwriter = new XMLWriter(); var xStr; initwriter.WriteString("<?xml version= '1.0'?>\n"); initwriter.WriteString("<!DOCTYPE INIT ["); initwriter.WriteString("<!ELEMENT INIT (FILE)*>"); initwriter.WriteString("<!ELEMENT FILE ( DEF, TAR)>"); initwriter.WriteString("<!ELEMENT DEF (#PCDATA)>"); initwriter.WriteString("<!ELEMENT TAR (#PCDATA)>"); initwriter.WriteString("]>"); initwriter.BeginNode('INIT'); initwriter.BeginNode('FILE'); initwriter.Node('DEF',label); initwriter.Node('TAR',link); initwriter.EndNode(); initwriter.EndNode(); initwriter.Close(); xStr=initwriter.ToString(); document.getElementById("txtHint").innerHTML=xStr; } If I say BeginNode('INIT'); or BeginNode("INIT"); The node does not show up? What is the right way to do this? WriteString does work, however. Any suggestions?
R.Carter
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
this code is very useful and decreases lot of manual code for developers while creating xml content. Thanks a lot for posting this
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi
I would like to use your XMLWriter class code in a commercial project I am working on, under what license have you released this code?
Thanks
Manna
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
im working for a company (was an intern last year) and im writing an interface to a google maps think im working on. May i use your code in my project? It's somewhat commercial (the map is for a state park)
How should i credit you?
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
when I try the test it didn't genarate output xml but genarates alert box saying error undefind. Please provide me an answer?
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Hi I'm new to coding and was wondering if I had this code contained in a javascript tags on a jsp page. How would I go about creating the var x = new XMLWriter(); ?
As i have it right now, i can only use var inside of the javascript corret? How do i create the javascript function XMLWriter as a java object?
Thanks for the help.
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
 |
After generating the XML, should it not write to a file?
I'm not sure if it is possible to write a file from javascript. I suspect not.
|
| Sign In·View Thread·PermaLink | 1.93/5 |
|
|
|
 |
|
 |
I want to open an xml file and write into it and then save the file. pls provide guidance if you have. Thanks.
Pravin Parmar, PravinParmar.ce@gmail.com
|
| Sign In·View Thread·PermaLink | 1.62/5 |
|
|
|
 |
|
 |
i m searching for the same despretly if you get it plz send me at vikassharma@mansainfotech.com
thanks..
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Can I suggest a small change to the code thus:
this.FormatXML = function(Str) { if (Str.replace){ return Str.replace(/&/g, "&").replace(/\"/g, """).replace(//g, ">"); } else { return Str; } } This will then work correctly if a variable that has been forced into a numeric state is passed in Str
http://mikeoncode.blogspot.com/
|
| Sign In·View Thread·PermaLink | 3.20/5 |
|
|
|
 |
|
|