当前位置:Gxlcms > AJAX > js装载xml文件然后发向服务器的实现代码

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

时间:2021-07-01 10:21:17 帮助过:52人阅读

前台: 
代码如下:
<!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>

人气教程排行