| |   Skin:   
      User: Not logged in 
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Featured Articles
Book Chapters
Training Courses
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
About Me
My Blog
HeadGeek Articles
Talking Portfolio
World Trip Pics

XML DOM: Inserting, Modifying and Deleting elements using VBScript
Author: David Silverlight
Published: 5/10/2005 12:27 AM
Category: XML
Summary: This snippet demonstrates the all too common functionality of adding, modifying and deleting elements in an xml document. The demo page (see below) displays the xml before and after the actions.

You can view the code below in action here

function doInsert() 
 dim objNode, objDOM, strXPath, objParentNode,  objChildNode
 'http://////    Create and display the original XML Document       ///////
 'Create an XML DOM object from the XML file
 Set objDOM  = CreateObject("Microsoft.XMLDOM") 
 objDOM.async = false 
 'Display the original XML values
 txtOrig.value = objDOM.xml
 'http://////    Add an attribute to the element where CustomerID = "ANATR"         ///////
 'http://////////////////////////////// 'Select the node for which CustomerID = "ANTON" Note that we select attribute values using the "@"
 'Define the XPath that references the element where CustomerID = 'ANATR'
 strXPath = "/customers/customer[@CustomerID='ANATR']"
 'Create a reference to the element for the XPath Query
 Set objParentNode = CreateObject("Microsoft.XMLDOM")
 set objParentNode = objDOM.selectSingleNode(strXPath)
 'Add an attribute with a name="email" and a value = ""
 AddNodeAttribute objDOM, objParentNode, "email", ""
 'Display the contents of the XML DOM with the New Attribute added
 txtNew.value = objDOM.xml
 'http://////    Add a child element to the element modified above (where CustomerID = "ANATR")          ///////
 'Create a new element with the name of "abc"
 AddDomElement objParentNode, "abc", "Value of New Element"
 'Display the contents of the XML DOM with the New Attribute added AND the new child element
 txtNewWithChild.value = objDOM.xml
end function
Sub AddDomElement(objParentNode, strName, strValue)
 Dim objDOM, objNode
 Set objDOM  = CreateObject("Microsoft.XMLDOM")  
 Set objNode = objDOM.createElement(strName)
 objNode.Text = strValue
 objParentNode.appendChild objNode
End Sub
Sub AddNodeAttribute(objDom, objNode, strName, strValue)
 Dim objAttrib
 Set objAttrib = objDOM.createAttribute(strName)
 objAttrib.Text =strValue
 objNode.Attributes.setNamedItem objAttrib
 objDOM.documentElement.appendChild objNode
End Sub


Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”

Advertise on XMLPitstop

Advertise on XMLPitstop

EggHead Cafe
Web Servicee development
Buy text ads here!
online secure backup
Color Laser Printer
Giorgio Armani sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


David Silverlight's| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139