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

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...