NO.1--建立一個XML數據庫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--選定節點SelectNode
你想操作哪個Node,必須定位到這個節點是不是,先看看這個data.xml有幾個Node??
用一個遞歸函數搞定:
getnodes(xmldoc)sub getnodes(node)dim iresponse.write("<br><b>NodeName:</b>"&node.nodename&"<br><b>NodeTypeString:</b>"&node.nodetypestring&"<br><b>NodeValue:</b>"&node.nodevalue&"<br><b>Text:</b>"&node.text&"<br><b>node.childnodes.length:</b>"&node.childnodes.length&"<p>")if node.childnodes.length<>0 thenfor i=0 to node.childnodes.length-1getnodes(node.childnodes(i))nextend ifend 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
是不是定位很簡單呀,還有個方法,比如定位<name>
xmldoc.selectsinglenode("http://name")NO.4--給節點賦值(修改節點的值)
學會了定位節點,利用其屬性,就可以修改或者賦值了
例如,把<name>的值caca改為wawa
xmldoc.selectsinglenode("http://name").text="wawa"xmldoc.save(server.mappath("data.xml"))NO.5--創建新的節點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--刪除一個節點DeleteNode
你必須明確你想刪除的這個節點的父節點,以及這個節點的特征
例如:刪除<qq>節點
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"))
新聞熱點
疑難解答