急啊!xml 问题。

Rossetti 2004-08-24 11:56:00
javascript 可以读去服务端的xml ,但不能修改是吗?

比如:

1.js 和 1.xml 到在服务器上同一个文件夹

js 可以读去 xml 中的内容,但怎么修改呀?
...全文
176 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rossetti 2004-08-26
  • 打赏
  • 举报
回复
嗯。改成 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) ;
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>
Jason627 2004-08-25
  • 打赏
  • 举报
回复
客户端怎么能去修改服务器文件呢?javascript是客户端代码,在本机,如果可以修改的话那就会乱套的,建议提交后台用ASP修改
LxcJie 2004-08-24
  • 打赏
  • 举报
回复
js可以修改xml文件,但是访问服务器的文件这样的情况我没试过,但应该会碰到拒绝访问错误,因为js运行在客户端,让其访问服务器的文件会遭到拒绝

所以建议使用后台代码修改xml文件
wanghr100 2004-08-24
  • 打赏
  • 举报
回复
修改,应该要把数据提交了才行.
可以用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>
fason 2004-08-24
  • 打赏
  • 举报
回复
use DOM, XmlHttp to send the DOMDocument back to server,and save

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧