asp 编辑xml子节点
xml文件<splashgrid><images><imagesmall="a1"large="b1"/><imagesmall="a2"large="b2"/></im...
xml 文件
<splashgrid>
<images>
<image small="a1" large="b1"/>
<image small="a2" large="b2"/>
</images>
<sketches>
<image small="c1"/>
<image small="c2"/>
</sketches>
</splashgrid>
我想在asp里面动态从数据库读出后写入images,每次开始判断images里有没有image,如果有,把image全部删掉,重新从数据库里面读最新的出来再保存,我做到这里的问题是不会循环删image,导致每次都把images这个节点全部删掉,就报错了……因为平时用到xml比较少,对里面的对象方法不太熟悉,试了很久,有点晕乎乎的了,上来求助~~希望高手能指点下,谢谢^_^ 展开
<splashgrid>
<images>
<image small="a1" large="b1"/>
<image small="a2" large="b2"/>
</images>
<sketches>
<image small="c1"/>
<image small="c2"/>
</sketches>
</splashgrid>
我想在asp里面动态从数据库读出后写入images,每次开始判断images里有没有image,如果有,把image全部删掉,重新从数据库里面读最新的出来再保存,我做到这里的问题是不会循环删image,导致每次都把images这个节点全部删掉,就报错了……因为平时用到xml比较少,对里面的对象方法不太熟悉,试了很久,有点晕乎乎的了,上来求助~~希望高手能指点下,谢谢^_^ 展开
1个回答
展开全部
set objDom=server.CreateObject("Microsoft.xmldom")
objDom.load(server.mappath("xml文件"))
set objnodes=objdom.documentElement.SelectSingleNode("//splashgrid/images").ChildNodes '取出images里面所有image
iLength = objnodes.length
response.Write(iLength)
do while iLength>0
set objparentnode=objdom.documentElement.SelectSingleNode("//splashgrid/images")'要删除的节点的父节点
set objnode=objdom.documentElement.SelectSingleNode("//images/image")'要删除的节点
objparentnode.removeChild objnode
objDom.save(Server.MapPath("xml文件"))
iLength=iLength-1
loop
'给节点添加子节点
set newNode=objdom.documentElement.SelectSingleNode("//splashgrid/images")
Set newnodeChild=objDom.CreateElement("image")
newnode.appendChild newnodeChild
'给节点添加属性
Set newsmall=objDom.CreateNode("attribute","small","")
Set newlarge=objDom.CreateNode("attribute","large","")
newnodeChild.SetAttributeNode newsmall
newnodeChild.SetAttributeNode newlarge
objDom.load(server.mappath("xml文件"))
set objnodes=objdom.documentElement.SelectSingleNode("//splashgrid/images").ChildNodes '取出images里面所有image
iLength = objnodes.length
response.Write(iLength)
do while iLength>0
set objparentnode=objdom.documentElement.SelectSingleNode("//splashgrid/images")'要删除的节点的父节点
set objnode=objdom.documentElement.SelectSingleNode("//images/image")'要删除的节点
objparentnode.removeChild objnode
objDom.save(Server.MapPath("xml文件"))
iLength=iLength-1
loop
'给节点添加子节点
set newNode=objdom.documentElement.SelectSingleNode("//splashgrid/images")
Set newnodeChild=objDom.CreateElement("image")
newnode.appendChild newnodeChild
'给节点添加属性
Set newsmall=objDom.CreateNode("attribute","small","")
Set newlarge=objDom.CreateNode("attribute","large","")
newnodeChild.SetAttributeNode newsmall
newnodeChild.SetAttributeNode newlarge
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询