Click here to Skip to main content
14,734,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Someone plz help me out with this, how to append data in xml file from html form data using javascript,Thanx in advance
Hi, i have tried different codes ,one of them is here below
<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME = 'D:\\IE Try\\PersonXML2.xml';

function SaveXMLData() {
var file = fso.CreateTextFile('D:\\IE Try\\PersonXML2.xml', true);
file.WriteLine('<!--?xmlversion="1.0" encoding="utf-8" ?-->\n'); file.WriteLine('<personinfo>\n');

file.Write(' <Person ');
file.Write('Name="' + document.getElementById('txtname').value + '" ');
file.Write('Address="' + document.getElementById('txtadd').value + '" ');
file.Write('Address="' + document.getElementById('txtage').value + '" ');

Name :
<input type="text" name="name" value="" id="txtname" /><br />
Address :
<input type="text" name="id" value="" id="txtadd" /><br />
<input type="text" name="write" value="" id="txtage" /><br />
<input type="button" onclick="SaveXMLData()" value="submit";
the above code generates an xml file but everytime it rewrites the inserted row, i mean it does'nt append the rows in xml file it always creates a new row deleting the previous once,
i need the xml file to append rows in it each time we execute html file.this code works only with IE when you change the settings in IE
1. Go to Tools > Internet options > Security > Custom Level
2. Under the ActiveX controls and plug-ins, select Enable for Initializing and Script ActiveX controls not marked as safe


I have tried with below code,even this is not working !!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Storing In XML</title>
<form id="myform" name="myform" action="#" method="get">
XML Document:<br />
<textarea id="showxml" name="showxml" rows="10" cols="40"></textarea>
<br /><br /><br />
Subject: <input id="namefield" type="text" name="namefield"><br>
Attachment: <input id="attachfield" type="text" name="attachfield"><br>
<input type="Submit" value="add record" önclick="addElement();document.myform.showxml.value='';
display(xmldoc.documentElement);" />
<input type="button" value="redisplay XML document"
display(xmldoc.documentElement);" />
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

function addElement()
var rootElement = document.documentElement;

var sname = document.getElementById('namefield').value;
var aname = document.getElementById('attachfield').value;

/* create subject element*/
var newSubject = document.createElement('subject');

/* create child elements and text values and append one by one */
var newName = document.createElement('sname');
var newNameText = document.createTextNode(sname);

var newAttachment = document.createElement('aname');
var newAttachmentText = document.createTextNode(aname);

/* append completed record to the document */


plz help me out!!!
Updated 10-Oct-14 1:26am
Sinisa Hajnal 10-Oct-14 4:54am
What have you tried before? Ask when you do some research and show us the code that doesn't work as you expect it to.
Sinisa Hajnal 10-Oct-14 6:13am
It doesn't rewrite inserted row, you're creating the file again and again. Instead OPEN the file (not CREATE it).

Also, please use Improve question link and move all code from the comments into the question for easier reading. Thank you.

1 solution

Hi You could try the code below to write HTML form data to XML file
< CategoryList> <category id="01">
 <description>This is a list my XML articles.</description> <active>true</active>
 < /CategoryList>
Here's the code:
< %@ Import Namespace="System.Data" %>
< %@ Import Namespace="System.Xml" %>
< %@ Page Language="C#" Debug="true" %>
<script runat="server"> void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack){ XmlDocument xmlDoc = new XmlDocument();

// Write down the XML declaration 
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);

// Create the root element 
XmlElement rootNode = xmlDoc.CreateElement("CategoryList"); xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement); xmlDoc.AppendChild(rootNode);

// Create a new <category> element and add it to the root node 
XmlElement parentNode = xmlDoc.CreateElement("Category");

// Set attribute name and value! 
parentNode.SetAttribute("ID", "01");

// Create the required nodes 
XmlElement mainNode = xmlDoc.CreateElement("MainCategory"); 
XmlElement descNode = xmlDoc.CreateElement("Description"); 
XmlElement activeNode = xmlDoc.CreateElement("Active");

// retrieve the text 
XmlText categoryText= xmlDoc.CreateTextNode("XML"); 
XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles."); 
XmlText activeText = xmlDoc.CreateTextNode("true");

// append the nodes to the parentNode without the value 

// save the value of the fields into the nodes 

// Save to the XML file 
xmlDoc.Save( Server.MapPath("categories.xml"));
Response.Write("XML file created"); } }

Hope this might help you ! :)
DivyaNaidu486 10-Oct-14 6:33am
Thanks for the code but it will not help me as i am not suppose to use .aspx pages, my requirement is transferring Html form data (using javascript) to xml file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900