Click here to Skip to main content
15,884,099 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
<html>
<head>
<title>Test</title>
<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 + '" ');
file.WriteLine('></Person>\n');

file.WriteLine('</personinfo>\n');
file.Close();
}
</script>
</head>
<body>
Name :
<input type="text" name="name" value="" id="txtname" /><br />
Address :
<input type="text" name="id" value="" id="txtadd" /><br />
Age:
<input type="text" name="write" value="" id="txtage" /><br />
<input type="button" onclick="SaveXMLData()" value="submit";
</body>
</html>
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
I.E.
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"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Storing In XML</title>
</head>
<body>
<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"
önclick="document.myform.showxml.value='';
display(xmldoc.documentElement);" />
</form>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","page1.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

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);
newName.appendChild(newNameText);
newSubject.appendChild(newName);

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



/* append completed record to the document */
rootElement.appendChild(newSubject);
xmlDoc.save("page1.xml")
}


</body>
</html>

plz help me out!!!
Posted
Updated 10-Oct-14 0:26am
v5
Comments
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
XML
< CategoryList> <category id="01">
<maincategory>XML</maincategory>
 <description>This is a list my XML articles.</description> <active>true</active>
 </category>
 < /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");
xmlDoc.DocumentElement.PrependChild(parentNode);

// 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 
parentNode.AppendChild(mainNode); 
parentNode.AppendChild(descNode); 
parentNode.AppendChild(activeNode);

// save the value of the fields into the nodes 
mainNode.AppendChild(categoryText); 
descNode.AppendChild(descText); 
activeNode.AppendChild(activeText);

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


Hope this might help you ! :)
 
Share this answer
 
v2
Comments
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