asp控制xml數(shù)據(jù)庫的經(jīng)典代碼
NO.1--建立一個XML數(shù)據(jù)庫data.xml
<?xml version="1.0"?> <records> <record> <name>caca</name> <qq>154222225</qq> <email>root@3ney.com</email> </record> <records>
NO.2--建立對象CreateObject
建立data.xml的對象先
set xmldoc=server.createobjcet("microsoft.xmldom")
xmldoc.load(server.mappath("data.xml")NO.3--選定節(jié)點SelectNode
你想操作哪個Node,必須定位到這個節(jié)點是不是,先看看這個data.xml有幾個Node??
用一個遞歸函數(shù)搞定:
getnodes(xmldoc)
sub getnodes(node)
dim i
response.write("
<b>NodeName:</b>"&node.nodename&"
<b>NodeTypeString:</b>"&node.nodetypestring&"
<b>NodeValue:</b>"&node.nodevalue&"
<b>Text:</b>"&node.text&"
<b>node.childnodes.length:</b>"&node.childnodes.length&"<p>")
if node.childnodes.length<>0 then
for i=0 to node.childnodes.length-1
getnodes(node.childnodes(i))
next
end if
end sub用這個函數(shù)后,可以看到這個data.xml有10個Node
這些Node可以很簡單的定位:
xmldoc.childnodes(0) xmldoc.childnodes(1) xmldoc.childnodes(1).childnodes(0) xmldoc.childnodes(1).childnodes(0).childnodes(0) xmldoc.childnodes(1).childnodes(0).childnodes(0).text xmldoc.childnodes(1).childnodes(0).childnodes(1) xmldoc.childnodes(1).childnodes(0).childnodes(1).text xmldoc.childnodes(1).childnodes(0).childnodes(2) xmldoc.childnodes(1).childnodes(0).childnodes(2).text
是不是定位很簡單呀,還有個方法,比如定位<name>
xmldoc.selectsinglenode("http://name")NO.4--給節(jié)點賦值(修改節(jié)點的值)
學會了定位節(jié)點,利用其屬性,就可以修改或者賦值了
例如,把<name>的值caca改為wawa
xmldoc.selectsinglenode("http://name").text="wawa"
xmldoc.save(server.mappath("data.xml"))NO.5--創(chuàng)建新的節(jié)點CreatenewNode
用createelement或者createnode("","","")
例如:在record下新建個<age>,只需要一句就搞定:
xmldoc.selectsinglenode("http://record").appendchild(xmldoc.createelement("<age>"))給<age>賦值
xmldoc.selectsinglenode("http://age").text="20"
xmldoc.save(server.mappath("data.xml"))NO.6--刪除一個節(jié)點DeleteNode
你必須明確你想刪除的這個節(jié)點的父節(jié)點,以及這個節(jié)點的特征
例如:刪除<qq>節(jié)點
xmldoc.selectsinglenode("http://record").removechild(xmldoc.selectsinglenode("http://qq"))例如:刪除那個<name>=caca的<record>
xmldoc.selectsinglenode("http://records").removechild(xmldoc.selectsinglenode("http://record[name='caca']))
xmldoc.save(server.mappath("data.xml"))以上六段經(jīng)典代碼相信一定會對大家利用asp控制xml數(shù)據(jù)庫有所幫助。
相關文章
Microsoft VBScript 編譯器錯誤 錯誤 ''800a03e9'' 內存不夠的解決方法
今天訪問后臺的時候提示Microsoft VBScript 編譯器錯誤 錯誤 '800a03e9' 內存不夠,通過下面的方法簡單修復了下,特分享下,方便需要的朋友2015-07-07
asp下request.querystring("id")與request("id&quo
一下問題一天遇到2次,復制過來以供下次參考,一般來說還使用萬能的request("id")比較好2008-01-01
用asp實現(xiàn)網(wǎng)址和郵件地址的轉換函數(shù)
用asp實現(xiàn)網(wǎng)址和郵件地址的轉換函數(shù)...2007-11-11

