如何使用javascript从html表单数据中附加xml文件中的数据 [英] how to append data in xml file from html form data using javascript

查看:64
本文介绍了如何使用javascript从html表单数据中附加xml文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人plz帮我解决这个问题,如何使用javascript,Thanx提前从html表单数据中将数据附加到xml文件中

您好,我尝试了不同的代码,其中一个在下面

< html>

< head>

< title>测试< / title>

< ; script type =text / javascript>

var xmlDoc = new ActiveXObject(Microsoft.XMLDOM);

var fso = new ActiveXObject(Scripting.FileSystemObject );

var FILENAME ='D:\\IE Try\\PersonXML2.xml';



函数SaveXMLData (){

var file = fso.CreateTextFile('D:\\IE Try\\PersonXML2.xml',true);

file.WriteLine ('<! - ?xmlversion =1.0encoding =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>

名称:

< input type =textname =namevalue =id = txtname/>< br />

地址:

< input type =textname =idvalue =id = txtadd/>< br />

年龄:

< inp ut type =textname =writevalue =id =txtage/>< br />

< input type =buttononclick =SaveXMLData( )value =submit;

< / body>

< / html>

以上代码生成一个xml文件但是每次重写插入的行时,我的意思是它不会在xml文件中附加行,它总是创建一个删除前一次的新行,

i需要xml文件在每次我们附加行时执行html文件。当您在IE中更改设置时,此代码仅适用于IE

IE

1.转到工具>互联网选项>安全>自定义级别

2.在ActiveX控件和插件下,选择启用初始化和脚本未标记为安全的ActiveX控件



= ================================================== ================================













我尝试使用以下代码,即使这不起作用!!!

<!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-Typecontent =text / html; charset = utf-8/>

< title> ;存储在XML< / title>

< / head>

< body>

< form id =myformname =myformaction =#method =get>

XML文档:< br />

< textarea id =showxmlname =showxmlrows =10cols =40>< / textarea&g t;

< br />< br />< br />

主题:< input id =namefieldtype =text name =namefield>< br>

附件:< input id =attachfieldtype =textname =attachfield>< br>

< input type =Submitvalue =add recordönclick=addElement(); document.myform.showxml.value ='';

display(xmldoc.documentElement) ; />

< input type =buttonvalue =重新显示XML文档
$ b $bönclick=document.myform.showxml.value ='';

display(xmldoc.documentElement); />

< / form>

< script>

if(window.XMLHttpRequest)

{// IE7 +,Firefox,Chrome,Opera,Safari代码

xmlhttp =新XMLHttpRequest();

}

其他

{// IE6代码,IE5

xmlhttp =新ActiveXObject(Microsoft.XMLHTTP);

}

xmlhttp.open(GET,page1.xml,false);

xmlhttp.send();

xmlDoc = xmlhttp.responseXML;



函数addElement()

{

var rootElement = document.documentElement;



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

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

< br $>


/ *创建主题元素* /

var newSubject = document.createElement('subject');





/ *创建子元素和文本值并逐个追加* /

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);







/ *将完成的记录追加到文件中* /

rootElement.appendChild(newSubject);

xmlDoc.save(page1。 xml)

}





< / body>

< ; / html>



plz帮帮我!!!

解决方案

你好b $ b你好可以尝试下面的代码将HTML表单数据写入XML文件

 <     CategoryList  >  <   category     id   =  01 >  
< maincategory > XML < / maincategory >
< description > 这是我的XML文章列表。< / description > < active > true < / active >
< span class =code-keyword>< / category >
< / CategoryList >
以下是代码:
< %@ 导入 命名空间 = System.Data >
< %@ 导入 命名空间 = 系统。 Xml >
< %@ 语言 = C# 调试 = true >
< script runat = server > void Page_Load(object sender,System.EventArgs e){if(!Page.IsPostBack){XmlDocument xmlDoc = new XmlDocument();

//记下XML声明
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(1.0,utf-8,null);

//创建根元素
XmlElement rootNode = xmlDoc.CreateElement(CategoryList); xmlDoc.InsertBefore(xmlDeclaration,xmlDoc.DocumentElement); xmlDoc.AppendChild(根节点);

//创建一个新的< category > 元素并将其添加到根节点
XmlElement parentNode = xmlDoc.CreateElement(Category);

//设置属性名称和值!
parentNode.SetAttribute(ID,01);
xmlDoc.DocumentElement.PrependChild(parentNode);

//创建所需的节点
XmlElement mainNode = xmlDoc.CreateElement(MainCategory);
XmlElement descNode = xmlDoc.CreateElement(Description);
XmlElement activeNode = xmlDoc.CreateElement(Active);

//检索文本
XmlText categoryText = xmlDoc.CreateTextNode(XML);
XmlText descText = xmlDoc.CreateTextNode(这是我的XML文章列表。);
XmlText activeText = xmlDoc.CreateTextNode(true);

//将节点附加到parentNode而没有值
parentNode.AppendChild(mainNode);
parentNode.AppendChild(descNode);
parentNode.AppendChild(activeNode);

//将字段的值保存到节点
mainNode.AppendChild(categoryText);
descNode.AppendChild(descText);
activeNode.AppendChild(activeText);

//保存到XML文件
xmlDoc.Save(Server.MapPath(categories.xml));
Response.Write(XML file created); }
< / category >





希望这对您有所帮助! :)


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!!!

解决方案

Hi You could try the code below to write HTML form data to XML file

< 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 ! :)


这篇关于如何使用javascript从html表单数据中附加xml文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆