急啊!xml 问题。 js可以修改xml文件,但是访问服务器的文件这样的情况我没试过,但应该会碰到拒绝访问错误,因为js运行在客户端,让其访问服务器的文件会遭到拒绝 所以建议使用后台代码修改xml文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 use DOM, XmlHttp to send the DOMDocument back to server,and save 修改,应该要把数据提交了才行.可以用Form,或是XMLHTTP来提交.最好直接用服务器端的来修改.demo.asp<% @Language="JavaScript" %><%var xmlDoc =Server.CreateObject("Msxml2.DOMDocument")//加载XML文件xmlDoc.load(Server.MapPath("bao.xml"))var root = xmlDoc.documentElement//创建新节点var newNode = xmlDoc.createNode(1,"book","")newNode.text = "book0"//插入节点var currNode = root.insertBefore(newNode,root.childNodes.item(0))Response.ContentType = "text/xml"Response.Write(root.xml)//保存xmlDoc.save(Server.MapPath("bao.xml"))%>bao.xml<?xml version="1.0" encoding="gb2312"?><books> <book>book1</book> <book>book2</book> <book>book3</book></books> 客户端怎么能去修改服务器文件呢?javascript是客户端代码,在本机,如果可以修改的话那就会乱套的,建议提交后台用ASP修改 嗯。改成 webservice 了。server 端----------------------------------public string FilePath1 = "/Stat.xml" ;public void CreateXml( string ID , string Name ){string sName = Server.UrlEncode(Name).ToString();string FilePath = getVD() + FilePath1 ;string fileName = FilePath;XmlTextWriter tw = new XmlTextWriter(fileName,null);tw.Formatting = Formatting.Indented;tw.WriteStartDocument();tw.WriteStartElement("StatData");tw.WriteAttributeString("DataType","统计");tw.WriteStartElement("Node");tw.WriteAttributeString("ID",ID) ;tw.WriteAttributeString("Name",sName);tw.WriteElementString("Num", "1") ;tw.WriteEndElement();tw.WriteEndElement();tw.WriteEndDocument();tw.Flush();tw.Close();}[WebMethod]public void UpdateXml( string ID , string Name ){sName = HttpUtility.UrlDecode(Name,System.Text.Encoding.GetEncoding("gb2312")).ToString() ;string FilePath = getVD() + FilePath1 ;bool b = false ;if(!File.Exists(FilePath))CreateXml( ID , Name ) ;else{System.Xml.XmlDocument xd = new XmlDocument() ;xd.Load(FilePath) ;XmlNodeList nodeList = xd.SelectSingleNode("StatData").ChildNodes;foreach(XmlNode xn in nodeList){XmlElement xe = (XmlElement)xn;if( xe.GetAttribute("ID").ToString() == ID ){b = true ;xe.SetAttribute("Name",sName);XmlNodeList nls = xe.ChildNodes;foreach(XmlNode xn1 in nls){XmlElement xe2 = (XmlElement)xn1;if(xe2.Name == "Num"){xe2.InnerText =System.Convert.ToString(System.Convert.ToInt32(xe2.InnerText) + 1);break;}}break;}} if (b)xd.Save(FilePath) ;elseInsertXml( ID , Name ) ;}}public void InsertXml( string ID , string Name ){string sName = Server.UrlEncode(Name).ToString();string FilePath = getVD() + FilePath1 ;System.Xml.XmlDocument xd = new XmlDocument() ;xd.Load(FilePath) ;System.Xml.XmlNode xn = xd.DocumentElement.ChildNodes[0].CloneNode(true) ;xn.Attributes["ID"].Value = ID ;xn.Attributes["Name"].Value = sName ;xn["Num"].InnerText = "1" ;xd.DocumentElement.AppendChild(xn) ;xd.Save(FilePath) ;}public string getVD(){return Server.MapPath("").ToString() ;}client 端------------------------------------------------------<script language=javascript>function oninit1(){ service.useService("service.asmx?WSDL","myservice");}function UpdateXml(name){ service.myservice.callService("Stat",'tongji',name);}</script><body onload=oninit1();><div id=service style="BEHAVIOR: url(webservice.htc)" ></DIV><a onclick="UpdateXml('xxxx');" href="http://...../1.rar">xxxxx</a> onResize事件失效的问题.. CuteEditor 中无法创建有事件的按钮[100分] 文本框得到图片的路径以后自动显示图片 关于表格拖动的问题 求助opener对象的问题,帮帮我 求高手指教.........在线等回复 为什么用JQ给元素添加ID后不能用选择器操作? 求一个平均展示的算法 ueditor百度编辑器,输入链接,如何空格不自动添加A标签 element upload 上传图片到一定的数量之后 删除加号 如何判断文本框里输入的是浮点型? 在线等!如何判断一个文本框是否获得焦点!
可以用Form,或是XMLHTTP来提交.最好直接用服务器端的来修改.demo.asp<% @Language="JavaScript" %>
<%
var xmlDoc =Server.CreateObject("Msxml2.DOMDocument")
//加载XML文件
xmlDoc.load(Server.MapPath("bao.xml"))
var root = xmlDoc.documentElement
//创建新节点
var newNode = xmlDoc.createNode(1,"book","")
newNode.text = "book0"
//插入节点
var currNode = root.insertBefore(newNode,root.childNodes.item(0))
Response.ContentType = "text/xml"
Response.Write(root.xml)
//保存
xmlDoc.save(Server.MapPath("bao.xml"))
%>bao.xml<?xml version="1.0" encoding="gb2312"?>
<books>
<book>book1</book>
<book>book2</book>
<book>book3</book>
</books>
----------------------------------
public string FilePath1 = "/Stat.xml" ;public void CreateXml( string ID , string Name )
{
string sName = Server.UrlEncode(Name).ToString();
string FilePath = getVD() + FilePath1 ;
string fileName = FilePath;
XmlTextWriter tw = new XmlTextWriter(fileName,null);
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("StatData");
tw.WriteAttributeString("DataType","统计");
tw.WriteStartElement("Node");
tw.WriteAttributeString("ID",ID) ;
tw.WriteAttributeString("Name",sName);
tw.WriteElementString("Num", "1") ;
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();}[WebMethod]
public void UpdateXml( string ID , string Name )
{sName = HttpUtility.UrlDecode(Name,System.Text.Encoding.GetEncoding("gb2312")).ToString() ;
string FilePath = getVD() + FilePath1 ;
bool b = false ;
if(!File.Exists(FilePath))
CreateXml( ID , Name ) ;
else
{
System.Xml.XmlDocument xd = new XmlDocument() ;xd.Load(FilePath) ;XmlNodeList nodeList = xd.SelectSingleNode("StatData").ChildNodes;foreach(XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if( xe.GetAttribute("ID").ToString() == ID )
{
b = true ;
xe.SetAttribute("Name",sName);XmlNodeList nls = xe.ChildNodes;
foreach(XmlNode xn1 in nls)
{
XmlElement xe2 = (XmlElement)xn1;
if(xe2.Name == "Num")
{
xe2.InnerText =System.Convert.ToString(System.Convert.ToInt32(xe2.InnerText) + 1);
break;
}
}
break;
}
}
if (b)
xd.Save(FilePath) ;
else
InsertXml( ID , Name ) ;
}
}public void InsertXml( string ID , string Name )
{string sName = Server.UrlEncode(Name).ToString();string FilePath = getVD() + FilePath1 ;
System.Xml.XmlDocument xd = new XmlDocument() ;
xd.Load(FilePath) ;
System.Xml.XmlNode xn = xd.DocumentElement.ChildNodes[0].CloneNode(true) ;
xn.Attributes["ID"].Value = ID ;
xn.Attributes["Name"].Value = sName ;
xn["Num"].InnerText = "1" ;
xd.DocumentElement.AppendChild(xn) ;
xd.Save(FilePath) ;
}
public string getVD()
{
return Server.MapPath("").ToString() ;
}client 端
------------------------------------------------------
<script language=javascript>
function oninit1()
{
service.useService("service.asmx?WSDL","myservice");
}
function UpdateXml(name)
{
service.myservice.callService("Stat",'tongji',name);
}</script>
<body onload=oninit1();>
<div id=service style="BEHAVIOR: url(webservice.htc)" ></DIV>
<a onclick="UpdateXml('xxxx');" href="http://...../1.rar">xxxxx</a>