使用ASP添加xml节点功能
相关方法:
createNode(type, name, nameSpaceURI);//建立一个指定型态、名称,及命名空间的新节点
setAttribute( name , value );//在指定的节点插入一个新的属性,如果属性名已经存在,则替换原属性名的值
appendChild(newChild);//加上一个节点当作指定节点最后的子节点。
insertBefore(newChild,refChild);//在指定的节点前插入一个子节点。
xmlDocument.save(url);//保存操作.
实例:
Vbscript
引用:
set Doc = CreateObject("Microsoft.XMLDOM")
Doc.async = false
Doc.load(Server.MapPath("data.xml"))
set root = Doc.DocumentElement
set book = Doc.createNode(1,"book","")
set bookname = Doc.createNode(1,"name","")
set publisher = Doc.createNode(1,"publisher","")
set img = Doc.createNode(1,"img","")
bookname.text = "Coldfusion"
publisher.text = "上海教育出版社"
img.text = "img/5.jpg"
book.appendChild(bookname)
book.appendChild(publisher)
book.appendChild(img)
root.appendChild(book)
Doc.Save(Server.MapPath("data.xml"))
call Doc.getElementsByTagName("book").item(Doc.DocumentElement.childNodes.length-1).setAttribute("cost","200") '通过length来获取需要添加属性的节点所在的位置.
Doc.Save(Server.MapPath("data.xml"))
Xml
引用:
<data>
<book cost="56">
<name>Dreamweaver</name>
<publisher>上海科技出版社</publisher>
<img>img/dw.jpg</img>
</book>
<book cost="62">
<name>Flash</name>
<publisher>铁道出版社</publisher>
<img>img/flash.jpg</img>
</book>
<book cost="48">
<name>Firweorks</name>
<publisher>教育出版社</publisher>
<img>img/fw.jpg</img>
</book>
</data>