Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
 
Add your own
alternative version
Go to top

Writing XML using JavaScript

, 21 Dec 2005
This article illustrates a Javascript XMLWriter object.
xmlwriter_src.zip
<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>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 21 Dec 2005
Article Copyright 2005 by christophilus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid