數據庫現在已經成為網站的靈魂,沒有數據庫的網站不是一個真正的網站,然而數據庫需要和服務器頻繁交互,下面是錯新技術頻道小編帶給大家的asp控制xml數據庫的經典代碼,希望能幫到大家。
NO.1--建立一個XML數據庫data.xml
<?xml version="1.0"?>caca 154222225 root@3ney.com
NO.2--建立對象CreateObject
建立data.xml的對象先
set xmldoc=server.createobjcet("microsoft.xmldom")xmldoc.load(server.mappath("data.xml")
NO.3--選定節點SelectNode
你想操作哪個Node,必須定位到這個節點是不是,先看看這個data.xml有幾個Node??
用一個遞歸函數搞定:
?
getnodes(xmldoc)sub getnodes(node)dim iresponse.write("NodeName:"&node.nodename&"NodeTypeString:"&node.nodetypestring&"NodeValue:"&node.nodevalue&"Text:"&node.text&"node.childnodes.length:"&node.childnodes.length&"
") if node.childnodes.length<>0 then for i=0 to node.childnodes.length-1 getnodes(node.childnodes(i)) next end if end sub
用這個函數后,可以看到這個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).textxmldoc.childnodes(1).childnodes(0).childnodes(1)xmldoc.childnodes(1).childnodes(0).childnodes(1).textxmldoc.childnodes(1).childnodes(0).childnodes(2)xmldoc.childnodes(1).childnodes(0).childnodes(2).text
是不是定位很簡單呀,還有個方法,比如定位
xmldoc.selectsinglenode("http://name")
NO.4--給節點賦值(修改節點的值)
學會了定位節點,利用其屬性,就可以修改或者賦值了
例如,把
?
xmldoc.selectsinglenode("http://name").text="wawa"xmldoc.save(server.mappath("data.xml"))
NO.5--創建新的節點CreatenewNode
用createelement或者createnode("","","")
例如:在record下新建個
xmldoc.selectsinglenode("http://record").appendchild(xmldoc.createelement(""))
給
xmldoc.selectsinglenode("http://age").text="20"xmldoc.save(server.mappath("data.xml"))
NO.6--刪除一個節點DeleteNode
你必須明確你想刪除的這個節點的父節點,以及這個節點的特征
例如:刪除
xmldoc.selectsinglenode("http://record").removechild(xmldoc.selectsinglenode("http://qq"))
例如:刪除那個
xmldoc.selectsinglenode("http://records").removechild(xmldoc.selectsinglenode("http://record[name='caca']))xmldoc.save(server.mappath("data.xml"))
以上就是錯新技術頻道小編給大家整理的asp控制xml數據庫的經典代碼。通過錯新技術頻道小編的介紹,我們可以清楚的知道這些操作方法,希望能幫助到大家。
新聞熱點
疑難解答