Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...