欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js装载xml文件然后发向服务器的实现代码

程序员文章站 2022-03-31 13:57:10
前台: 复制代码 代码如下:
前台: 
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js装载xml文件然后发向服务器</title>
<script type="text/javascript"><!--
var xmlhttp = null;
function f(){
var xmldoc = new activexobject("msxml2.domdocument.3.0");
xmldoc.async = false;
xmldoc.load("xmlfile.xml");//只改了这里,原来是loadxml("");
sendxml( xmldoc,'default.aspx');
}
//向服务器发送xml文档
function sendxml(xmldoc,serverurl){
xmlhttp = new activexobject ("msxml2.xmlhttp.3.0");
var strdoc;
if (typeof(xmldoc) == "object")//这里的判断是需要的.这里仍然需要加个xml后缀
strdoc = xmldoc.xml;
else
strdoc = xmldoc;
xmlhttp.open ("post","default.aspx" ,true);
xmlhttp.onreadystatechange=getdata;
xmlhttp.send(strdoc);
}
function getdata(){
if (xmlhttp.readystate==4)
{
var strxml=xmlhttp.responsetext;
//这里接受服务器传来的xml文档,转化成xml文档.
var xmldoc = new activexobject("msxml2.domdocument.3.0");
xmldoc.async = false;
xmldoc.loadxml(strxml);
//alert(xmldoc.xml);
var singlenode = xmldoc.selectsinglenode("/root/person[gender='male']");//这里的值需要加引号
alert(singlenode.text);
}
}
// --></script>
</head>
<body>
<input type="button" onclick="f();" value="request" />
</body>
</html>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>js装载xml文件然后发向服务器</title>
<script type="text/javascript"><!--
var xmlhttp = null;
function f(){
var xmldoc = new activexobject("msxml2.domdocument.3.0");
xmldoc.async = false;
xmldoc.load("xmlfile.xml");//只改了这里,原来是loadxml("");
sendxml( xmldoc,'default.aspx');
}
//向服务器发送xml文档
function sendxml(xmldoc,serverurl){
xmlhttp = new activexobject ("msxml2.xmlhttp.3.0");
var strdoc;
if (typeof(xmldoc) == "object")//这里的判断是需要的.这里仍然需要加个xml后缀
strdoc = xmldoc.xml;
else
strdoc = xmldoc;
xmlhttp.open ("post","default.aspx" ,true);
xmlhttp.onreadystatechange=getdata;
xmlhttp.send(strdoc);
}
function getdata(){
if (xmlhttp.readystate==4)
{
var strxml=xmlhttp.responsetext;
//这里接受服务器传来的xml文档,转化成xml文档.
var xmldoc = new activexobject("msxml2.domdocument.3.0");
xmldoc.async = false;
xmldoc.loadxml(strxml);
//alert(xmldoc.xml);
var singlenode = xmldoc.selectsinglenode("/root/person[gender='male']");//这里的值需要加引号
alert(singlenode.text);
}
}
// --></script>
</head>
<body>
<input type="button" onclick="f();" value="request" />
</body>
</html>

ajax
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.xml;
public partial class testxml_default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load(request.inputstream);//接收到客户端传来的xml
xmldoc.save(server.mappath("~"+"/hello.xml"));
response.write(xmldoc.innerxml);//返回修改后的xml文档
response.end();
}
}
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.xml;
public partial class testxml_default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load(request.inputstream);//接收到客户端传来的xml
xmldoc.save(server.mappath("~"+"/hello.xml"));
response.write(xmldoc.innerxml);//返回修改后的xml文档
response.end();
}
}

xml文档:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<person id="1">
<name>tree</name>
<gender>male</gender>
</person>
</root>

上一篇: 爸爸挨饿

下一篇: 四大窝囊汉子