ASP操作XML資料小結

來源:互聯網
上載者:User

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 i
         response.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 then
                  for i=0 to node.childnodes.length-1
                  getnodes(node.childnodes(i))
              next
       end if
       end sub
       用這個函數後,可以看到這個data.xml有10個Node
       這些Node可以很簡單的定位:
       xmldoc
       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("//name")
       還有:
      xmldoc.getelementsbytagname("name").item(0)
      
NO.4--給節點賦值(修改節點的值)
       學會了定位節點,利用其屬性,就可以修改或者賦值了
       例如,把<name>的值caca改為wawa
       xmldoc.selectsinglenode("//name").text="wawa"
       xmldoc.save(server.mappath("data.xml"))
       搞定!
NO.5--建立新的節點CreatenewNode
       用createelement或者createnode("","","")
       例如:在record下建立個<age>,只需要一句就搞定:
       xmldoc.selectsinglenode("//record").appendchild(xmldoc.createelement("<age>"))
       給<age>賦值
       xmldoc.selectsinglenode("//age").text="20"
       xmldoc.save(server.mappath("data.xml"))
       搞定!
NO.6--刪除一個節點DeleteNode
       你必須明確你想刪除的這個節點的父節點,以及這個節點的特徵
       例如:刪除<qq>節點
       xmldoc.selectsinglenode("//record").removechild(xmldoc.selectsinglenode("//qq"))
       例如:刪除那個<name>=caca的<record>
       xmldoc.selectsinglenode("//records").removechild(xmldoc.selectsinglenode("//record[name='caca']))
       xmldoc.save(server.mappath("data.xml"))
       搞定!
只有能熟練這6條code,用asp控制xml資料庫,也就差不多了...

這裡有個留言本的例子,可以參考一下
下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.