You are on page 1of 9

The following javascript code and htm file can be used to create an XML writer [1]

Global.js

function JSettings()

this.IE=document.all?true:false;

this.MouseX=_JSettings_MouseX;

this.MouseY=_JSettings_MouseY;

this.SrcElement=_JSettings_SrcElement;

this.Parent=_JSettings_Parent;

this.RunOnLoad=_JSettings_RunOnLoad;

this.FindParent=_JSettings_FindParent;

this.FindChild=_JSettings_FindChild;

this.FindSibling=_JSettings_FindSibling;

this.FindParentTag=_JSettings_FindParentTag;

function _JSettings_MouseX(e)

{return this.IE?event.clientX:e.clientX;}

function _JSettings_MouseY(e)

{return this.IE?event.clientY:e.clientY;}

function _JSettings_SrcElement(e)

{return this.IE?event.srcElement:e.target;}

function _JSettings_Parent(Node)

{return this.IE?Node.parentNode:Node.parentElement;}

function _JSettings_RunOnLoad(Meth){var Prev=(window.onload)?window.onload:function()


{};window.onload=function(){Prev();Meth();};}

function _JSettings_FindParent(Node, Attrib, Value)


{var Root = document.getElementsByTagName("BODY")[0];

Node = Node.parentNode; while (Node != Root && Node.getAttribute(Attrib) != Value)


{Node=Node.parentNode;}

if (Node.getAttribute(Attrib) == Value) {return Node;} else {return null;}}

function _JSettings_FindParentTag(Node, TagName)

{var Root = document.getElementsByTagName("BODY")[0];

TagName=TagName.toLowerCase();

Node = Node.parentNode; while (Node != Root && Node.tagName.toLowerCase() != TagName)


{Node=Node.parentNode;}

if (Node.tagName.toLowerCase() == TagName) {return Node;} else {return null;}}

function _JSettings_FindChild(Node, Attrib, Value)

if (Node.getAttribute)

if (Node.getAttribute(Attrib) == Value) return Node;

var I=0;

var Ret = null;

for (I=0;I<Node.childNodes.length;I++)

Ret = FindChildByAttrib(Node.childNodes[I]);

if (Ret) return Ret;

return null;

function _JSettings_FindSibling(Node, Attrib, Value)

var Nodes=Node.parentNode.childNodes;

var I=0;
for (I=0;I<Nodes.length;I++)

if (Nodes[I].getAttribute)

if (Nodes[I].getAttribute(Attrib) == Value)

{return Nodes[I];}

return null;

var Settings = new JSettings();

XMLWriter.js

function XMLWriter()

this.XML=[];

this.Nodes=[];

this.State="";

this.FormatXML = function(Str)

if (Str)

return Str.replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g,


"&gt;");

return ""

}
//begins a node: <Name

this.BeginNode = function(Name)

if (!Name) return;

if (this.State=="beg") this.XML.push(">");

this.State="beg";

this.Nodes.push(Name);

this.XML.push("<"+Name);

//ends the current open node: />

this.EndNode = function()

if (this.State=="beg")

this.XML.push("/>");

this.Nodes.pop();

else if (this.Nodes.length>0)

this.XML.push("</"+this.Nodes.pop()+">");

this.State="";

//writes an attribute to the open node: <Node Name="Value"

this.Attrib = function(Name, Value)

{
if (this.State!="beg" || !Name) return;

this.XML.push(" "+Name+"=\""+this.FormatXML(Value)+"\"");

//writes out a sting value to the open node

this.WriteString = function(Value)

if (this.State=="beg") this.XML.push(">");

this.XML.push(this.FormatXML(Value));

this.State="";

//writes out a node and value: <Name>Value</Name>

this.Node = function(Name, Value)

if (!Name) return;

if (this.State=="beg") this.XML.push(">");

this.XML.push((Value=="" || !Value)?"<"+Name+"/>":"<"+Name+">"+this.FormatXML(Value)
+"</"+Name+">");

this.State="";

//ends any nodes that have not been ended

this.Close = function()

while (this.Nodes.length>0)

this.EndNode();

this.State="closed";
}

//returns the entire xml document as a string

this.ToString = function(){return this.XML.join("");}

XMLTest.htm

<html>

<head>

<script src="Global.js" language="javascript"></script>

<script src="XMLWriter.js" language="javascript"></script>

<script language="javascript" type="text/javascript">

function WriteTest()

try

var XML=new XMLWriter();

XML.BeginNode("Example");

XML.Attrib("SomeAttribute", "And Some Value");

XML.Attrib("AnotherAttrib", "...");

XML.WriteString("This is an example of the JS XML WriteString method.");

XML.Node("Name", "Value");

XML.BeginNode("SubNode");

XML.BeginNode("SubNode2");

XML.EndNode();

XML.BeginNode("SubNode3");
XML.WriteString("Blah blah.");

XML.EndNode();

XML.Close(); // Takes care of unended tags.

// The replace in the following line are only for making the XML look prettier in the textarea.

document.getElementById("ExampleOutput").value=XML.ToString().replace(/</g,"\n<");

catch(Err)

alert("Error: " + Err.description);

return false;

function WriteForm(e)

try

var Frm=Settings.SrcElement(e);

var XML=new XMLWriter();

XML.BeginNode(Frm.name);

XML.Attrib("Example", "Attribute & Value");

var Nodes=Frm.elements;

for (var i=0;i<Nodes.length;i++)

XML.Node(Nodes[i].name, Nodes[i].value);

XML.EndNode();

XML.Close();

document.getElementById("ExampleOutput").value=XML.ToString().replace(/</g,"\n<");
}

catch(Err)

alert("Error: " + Err.description);

return false;

</script>

</head>

<body>

Try entering values into the following fields, then click the Test button to see the

resulting XML.

<form name="User" method="post" action="#" onsubmit="return WriteForm(event);">

<table cellpadding="0" cellspacing="0">

<tr>

<td>First Name:</td>

<td><input type="text" name="FirstName" /></td>

</tr>

<tr>

<td>Middle Name:</td>

<td><input type="text" name="MiddleName" /></td>

</tr>

<tr>

<td>Last Name:</td>

<td><input type="text" name="LastName" /></td>

</tr>

<tr>
<td>Birth Date:</td>

<td>

<input type="text" name="BirthDate" />

<input type="submit" value="Test" />

</td>

</tr>

</table>

</form>

<a href="#" onclick="WriteTest();">Or click here to see another XML exmaple.</a>

<br /><br />

Output:

<form><textarea id="ExampleOutput" style="width:100%" rows="10"></textarea></form>

</body>

</html>

Sources
1. http://www.codeproject.com/KB/ajax/XMLWriter.aspx

You might also like