您好,登錄后才能下訂單哦!
本篇內容主要講解“js如何裝載xml文件然后發向服務器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“js如何裝載xml文件然后發向服務器”吧!
前臺:
復制代碼 代碼如下:
<!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>
到此,相信大家對“js如何裝載xml文件然后發向服務器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。