ASP操作XML数据小结

XML用途之一 



增加、删除、修改“XML 记录”的例子 

'XML 文件 

<?xml version="1.0" encoding="gb2312"?> 

<stocks date="2001-8-24"> 

<stock> 

<code>111</code> 

<name>AAA</name> 

<openprice>7.93</openprice> 

<currentprice>7.93</currentprice> 

<tradeamount>0</tradeamount> 

<totalamount>1000000</totalamount> 

<status>1</status> 

</stock> 

<stock> 

<code>222</code> 

<name>BBB</name> 

<openprice>170.92</openprice> 

<currentprice>170.92</currentprice> 

<tradeamount>0</tradeamount> 

<totalamount>100000</totalamount> 

<status>1</status> 

</stock> 

<stock> 

<code>333</code> 

<name>CCC</name> 

<openprice>55.43</openprice> 

<currentprice>55.43</currentprice> 

<tradeamount>0</tradeamount> 

<totalamount>100000</totalamount> 

<status>1</status></stock> 

</stocks> 



'处理文件 Stocks.asp 

<% 

Response.CacheControl = "no-cache" 

%> 

<html> 

<head> 



<title>XML 的股票行情</title> 

<style> 

td {font-size:9pt} 

</style> 

</head> 



<body> 

<h1>XML 的股票行情</h1> 



<hr> 

<% 

set xml = Server.CreateObject("MSXML2.domdocument") 

'set xml = Server.CreateObject("Microsoft.xmlDom") 

xml.load Server.MapPath("stocks.xml") 

if xml.parseError.errorCode <> 0 then 

Response.Write "xml.parseError.errorCode = " &xml.parseError.errorCode 

Response.Write "xml.parseError.reason = " &xml.parseError.reason 

Response.Write "xml.parseError.line = " &xml.parseError.line 

Response.End 

end if 



'如果<stocks>的DATE属性不是今天,则改为今天 

xmlDate = xml.documentElement.attributes.getNamedItem("date").text 

if datediff("d",xmlDate,date) >= 1 then 

'xml.documentElement.attributes.getNamedItem("date").text = FormatDateTime(date,2) 

'可以在此加入改变股票价格的代码 

i = 1 

StockAmount = xml.getElementsByTagName("stock").length 

set StockList = xml.getElementsByTagName("stock") 

for i = 0 to StockAmount - 1 

set StockTemp = StockList.item(i) 

randomize 

ss =1 

if rnd() > 0.6 then ss = -1 

randomize 

StockTemp.childNodes.item(2).text = FormatNumber(Ccur(StockTemp.childNodes.item(2).text)*(1+ss*rnd()/10),2) 

StockTemp.childNodes.item(3).text = StockTemp.childNodes.item(2).text 

next 



xml.save Server.MapPath("stocks.xml") 

end if 



if Request.Form("submit") = "增加股票" then 



set objRoot = xml.documentElement 

set newStock = xml.createElement("stock") 

objRoot.appendChild newStock 

set objA1 = xml.createElement("code") 

newStock.appendChild objA1 

objA1.text = Request.Form("scode") 

set objA2 = xml.createElement("name") 

newStock.appendChild objA2 

objA2.text = Request.Form("sname") 

set objA3 = xml.createElement("openprice") 

newStock.appendChild objA3 

objA3.text = Request.Form("openPrice") 

set objA4 = xml.createElement("currentprice") 

newStock.appendChild objA4 

objA4.text = Request.Form("curPrice") 

set objA5 = xml.createElement("tradeamount") 

newStock.appendChild objA5 

objA5.text = Request.Form("tradeAmount") 

set objA6 = xml.createElement("totalamount") 

newStock.appendChild objA6 

objA6.text = Request.Form("TotalAmount") 

set objA7 = xml.createElement("status") 

newStock.appendChild objA7 

objA7.text = Request.Form("status") 



xml.save Server.MapPath("stocks.xml") 



set newStock = nothing 

set objRoot = nothing 

set objA1 = nothing 

set objA2 = nothing 

set objA3 = nothing 

set objA4 = nothing 

set objA5 = nothing 

set objA6 = nothing 

set objA7 = nothing 



end if 



if Request.Form("submit") = "删除" and Request.Form("remove")<>empty then 

set objCodeList = xml.getElementsByTagName("code") 

remove = split(Request.Form("remove")) 

for i=0 to ubound(remove) 

for j=0 to objCodeList.Length-1 

if objcodeList.item(j).text = remove(i) then 

xml.documentElement.removeChild(objcodeList.item(j).parentNode) 

end if 

next 

xml.save Server.MapPath("stocks.xml") 

next 

end if 



'xml.save Server.MapPath("stocks.xml") 



Response.Write FormatDateTime(now) 

%> 

<hr> 

<form action="" method="post"> 

<TABLE cellSpacing=1 cellPadding=1 width="100%" border=1> 

<TR> 

<TD>股票代码</TD> 

<TD>股票名称</TD> 

<TD>开盘价格</TD> 

<TD>当前价格</TD> 

<TD>成交量</TD> 

<TD>流通总数</TD> 

<TD>状态</TD> 

<TD>删除</TD> 

</TR> 

<% 

i = 1 

StockAmount = xml.getElementsByTagName("stock").length 

set StockList = xml.getElementsByTagName("stock") 

for i = 0 to StockAmount - 1 

set StockTemp = StockList.item(i) 

%> 

<TR> 

<TD> <%=StockTemp.childNodes.item(0).text%></TD> 

<TD> <%=StockTemp.childNodes.item(1).text%></TD> 

<TD> <%=StockTemp.childNodes.item(2).text%></TD> 

<TD> <%=StockTemp.childNodes.item(3).text%></TD> 

<TD> <%=StockTemp.childNodes.item(4).text%></TD> 

<TD> <%=StockTemp.childNodes.item(5).text%></TD> 

<TD> <%=StockTemp.childNodes.item(6).text%></TD> 

<TD><input type="checkbox" name="remove" value=<%=StockTemp.childNodes.item(0).text%>></TD> 

</TR> 

<% 

next 

set xml = nothing 

%> 

<TR> 

<TD><input type="text" name="scode"></TD> 

<TD><input type="text" name="sname"></TD> 

<TD><input type="text" name="openPrice" size="6"></TD> 

<TD><input type="text" name="curPrice" size="6"></TD> 

<TD><input type="text" name="tradeAmount"></TD> 

<TD><input type="text" name="TotalAmount"></TD> 

<TD><input type="text" name="status" size="6"></TD> 

<TD><input type="submit" name="submit" value="删除"></TD> 

</TR> 

</TABLE> 

<% 

%> 

<input type="submit" name="submit" value="增加股票"> 

</form> 

</body> 

</html>  

来自: http://hi.baidu.com/yuyang0105/blog/item/82379d476713d32acefca3d8.html

你可能感兴趣的:(xml)