Be the first user to complete this post
|
Add to List |
VBA-Excel: Update XML File
In our earlier post we have seen how to Read Data from XML File using in Microsoft Excel. In this tutorial will teach you about how to update an XML file using Microsoft Excel.
I would recommend that you should this post before you continue -Read Data from XML File
Now there are various way you can update XML file -
- Update Single ParentNode
- Update attribute of a Node
- Add a new node under existing node
- Add new Attribute to the Node
Update Single ParentNode
Set TitleNode = oXMLFile.SelectSingleNode("/catalog/book[0]/title")
TitleNode.Text = "I am the new Title Here"
Update attribute of a Node
Set oAttribute = oXMLFile.SelectSingleNode("/catalog/book[0]/@id")
oAttribute.Text = "1111111111111111111111111111"
Add a new node under existing node
' select a parent node
Set ParentNode = oXMLFile.SelectSingleNode("/catalog/book[1]")
' add a new childNode
Set childNode = oXMLFile.CreateElement("NewNode")
childNode.Text = "I am The New NOde Here"
ParentNode.AppendChild (childNode)
Add new Attribute to the Node
'Add new Attribute to the Node
Set ParentNode = oXMLFile.SelectSingleNode("/catalog /book[2]/publish_date")
' add its attribute
Set newChildAttribute = oXMLFile.CreateAttribute("Status")
newChildAttribute.Text = "Very Old Book"
ParentNode.Attributes.SetNamedItem (newChildAttribute)
Complete Code:
Sub UpdateXML() Call fnUpdateXMLByTags End Sub Function fnUpdateXMLByTags() Dim mainWorkBook As Workbook Set mainWorkBook = ActiveWorkbook Set oXMLFile = CreateObject("Microsoft.XMLDOM") XMLFileName = "C:\Users\Sumit Jain\Google Drive\Excel Blog pics\XML" oXMLFile.Load (XMLFileName) Set TitleNodes = oXMLFile.SelectNodes("/catalog/book/title/text()") Set PriceNodes = oXMLFile.SelectNodes("/catalog/book/price/text()") 'Update Single ParentNode Set TitleNode = oXMLFile.SelectSingleNode("/catalog/book[0]/title") TitleNode.Text = "I am the new Title Here" 'Update attribute of a Node Set oAttribute = oXMLFile.SelectSingleNode("/catalog/book[0]/@id") oAttribute.Text = "1111111111111111111111111111" 'Add a new node under existing node ' select a parent node Set ParentNode = oXMLFile.SelectSingleNode("/catalog/book[1]") ' add a new childNode Set childNode = oXMLFile.CreateElement("NewNode") childNode.Text = "I am The New NOde Here" ParentNode.AppendChild (childNode) 'Add new Attribute to the Node Set ParentNode = oXMLFile.SelectSingleNode("/catalog /book[2]/publish_date") ' add its attribute Set newChildAttribute = oXMLFile.CreateAttribute("Status") newChildAttribute.Text = "Very Old Book" ParentNode.Attributes.SetNamedItem (newChildAttribute) oXMLFile.Save (XMLFileName) End Function
Also Read:
- VBA-Excel: Get the Instance of already opened word document
- VBA-Excel: Format already written text in a word document – Format Paragraphs
- VBA-Excel: Consolidator – Merge or Combine Multiple Excel Files Into One
- VBA-Excel: Perform Google Search on Internet Explorer using Microsoft Excel
- VBA-Excel: Create or Add Worksheets at the Run time.