Académique Documents
Professionnel Documents
Culture Documents
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;}
TagName=TagName.toLowerCase();
if (Node.getAttribute)
var I=0;
for (I=0;I<Node.childNodes.length;I++)
Ret = FindChildByAttrib(Node.childNodes[I]);
return null;
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;
XMLWriter.js
function XMLWriter()
this.XML=[];
this.Nodes=[];
this.State="";
this.FormatXML = function(Str)
if (Str)
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);
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="";
{
if (this.State!="beg" || !Name) return;
this.XML.push(" "+Name+"=\""+this.FormatXML(Value)+"\"");
this.WriteString = function(Value)
if (this.State=="beg") this.XML.push(">");
this.XML.push(this.FormatXML(Value));
this.State="";
if (!Name) return;
if (this.State=="beg") this.XML.push(">");
this.XML.push((Value=="" || !Value)?"<"+Name+"/>":"<"+Name+">"+this.FormatXML(Value)
+"</"+Name+">");
this.State="";
this.Close = function()
while (this.Nodes.length>0)
this.EndNode();
this.State="closed";
}
XMLTest.htm
<html>
<head>
function WriteTest()
try
XML.BeginNode("Example");
XML.Attrib("AnotherAttrib", "...");
XML.Node("Name", "Value");
XML.BeginNode("SubNode");
XML.BeginNode("SubNode2");
XML.EndNode();
XML.BeginNode("SubNode3");
XML.WriteString("Blah blah.");
XML.EndNode();
// 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)
return false;
function WriteForm(e)
try
var Frm=Settings.SrcElement(e);
XML.BeginNode(Frm.name);
var Nodes=Frm.elements;
XML.Node(Nodes[i].name, Nodes[i].value);
XML.EndNode();
XML.Close();
document.getElementById("ExampleOutput").value=XML.ToString().replace(/</g,"\n<");
}
catch(Err)
return false;
</script>
</head>
<body>
Try entering values into the following fields, then click the Test button to see the
resulting XML.
<tr>
<td>First Name:</td>
</tr>
<tr>
<td>Middle Name:</td>
</tr>
<tr>
<td>Last Name:</td>
</tr>
<tr>
<td>Birth Date:</td>
<td>
</td>
</tr>
</table>
</form>
Output:
</body>
</html>
Sources
1. http://www.codeproject.com/KB/ajax/XMLWriter.aspx