Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript XML HTML
I have a html form in which user fills the data.
That data is shown in xml file using javascript.
Posted 13-Mar-13 20:46pm
Edited 13-Mar-13 21:07pm
(no name)113.6K
v2
Comments
Shubhashish_Mandal at 14-Mar-13 2:04am
   
you need a xml parser to create a xml file along with the form data. I just google it with "javascript to create xml" and I get answer.
Tadit Dash at 14-Mar-13 2:08am
   
What have you tried so far ?
Post some code.
DivyaNaidu486 at 9-Oct-14 8:59am
   
someone plz help me, i have tried this code but i am not able to generate page1.xml file,even if i create page1.xml file manually, records are not getting appended to it from html page.Thanks in advance!!!
   
Hi Divya,
 
Please add another question describing your scenario and to support that, add some code snippets.
 
In that way, you will get more answers. Nobody would reply here because this is one old thread.
 
Thanks,
Tadit
DivyaNaidu486 at 10-Oct-14 3:17am
   
Hi Tadit,
The code i have used is
<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="submit" onclick="SaveXMLData()" />
</body>
</html>
The above code is generating an XML file ,every time i execute the html file it generates a new xml file with one row of data in it.
I need to append data to xml file , i mean every-time i execute html file ,it should add a new row to the same xml file.
Hope you will help me out with this!!!
and thanx for your Reply.
one more thing,the above code which i have posted runs only in IE Browser, that too after making some settings in IE.
settings are
In order to run safely you need to change setting of 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
 
   
Hi Divya,

Please add another question describing your scenario and to support that, add some code snippets.

In that way, you will get more answers. Nobody would reply here because this is one old thread.

Thanks,
Tadit
priya2013 at 14-Mar-13 5:23am
   
i hav tried this
<!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>New Web Project</title>
</head>
<body>

New Web Project Page

<form id="myform" name="myform" action="#" method="get">
XML Document:<br />
<textarea id="showxml" name="showxml" rows="10" cols="40"></textarea>
<br /><br /><br />
Name: <input type="text" name="namefield" id="namefield" size="50" /><br />
Title: <input type="text" name="titlefield" id="titlefield" size="30" />
<br />
Phone: <input type="text" name="phonefield" id="phonefield" size="20" />
<br />
Email: <input type="text" name="emailfield" id="emailfield" size="20" />
<br />
 
<input type="button" value="add record"
önclick="addElement();document.myform.showxml.value='';
display(xmldoc.documentElement);" />
 
<input type="button" value="delete last record"
önclick="deleteLastElement();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 name = document.getElementById('namefield').value;
var title = document.getElementById('titlefield').value;
var phone = document.getElementById('phonefield').value;
var email = document.getElementById('emailfield').value;
 
/* create employee element*/
var newEmployee = document.createElement('employee');
 

/* create child elements and text values and append one by one */
var newName = document.createElement('name');
var newNameText = document.createTextNode(name);
newName.appendChild(newNameText);
newEmployee.appendChild(newName);
 
var newTitle = document.createElement('title');
var newTitleText = document.createTextNode(title);
newTitle.appendChild(newTitleText);
newEmployee.appendChild(newTitle);

var newPhone = document.createElement('phone');
var newPhoneText = document.createTextNode(phone);
newPhone.appendChild(newPhoneText);
newEmployee.appendChild(newPhone);

var newEmail = document.createElement('email');
var newEmailText = document.createTextNode(email);
newEmail.appendChild(newEmailText);
newEmployee.appendChild(newEmail);

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

 
</body>
</html>
priya2013 at 14-Mar-13 5:24am
   
can u just send me the link from where u gt it
Shubhashish_Mandal at 14-Mar-13 9:04am
   
to whom you ask this?
priya2013 at 15-Mar-13 0:39am
   
i ask u shubhashish to send me the link
Shubhashish_Mandal at 22-Mar-13 6:16am
   
I told you before in my first comment to google it.

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100