Matteo NNZ
Matteo NNZ

Reputation: 12655

Create brand new XML node to append as child to other node

I would like to create a new XML node to append to an existing one in my XML file. Specifically, the structure of the file is:

<contract>
    <trade></trade>
    <trade></trade>
</contract> 

My idea is to get each <trade> node and append a new child to it. This child should look like this:

<tradeSource></tradeSource>

My question is, how do I define this new child to append? It doesn't seem I can find the right object to create on VBA (although the library MSXML v3.0 has been referenced in the project) and I don't manage to find such a sample of brand-new node anywhere on the web. My pseudo-code:

XMLFile.Load(myFileFullName)
Set tradeNodes = XMLFile.getElementsByTagName("trade")
For Each trade In tradeNodes 
    Set newNode = ???? '<-- how to fill this?
    trade.appendChild(newNode)
Next trade

Upvotes: 2

Views: 5819

Answers (1)

user6432984
user6432984

Reputation:

This should work:

Set newNode= XMLFile.CreateElement("price");
newNode.InnerText = "19.95"
trade.appendChild(newNode)

Please note that both variables trade and newNode should be declared as IXMLDOMNode (type defined in the library msxml6.dll).

Upvotes: 3

Related Questions