欢迎光临
我们一直在努力

ASP操作XML数据小结-.NET教程,XML应用

建站超值云服务器,限时71元/月

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数据库,也就差不多了…

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP操作XML数据小结-.NET教程,XML应用
分享到: 更多 (0)

相关推荐

  • 暂无文章