创建全新的XML节点作为子节点附加到其他节点 [英] Create brand new XML node to append as child to other node
问题描述
我想创建一个新的XML节点,以附加到XML文件中的现有节点上. 具体来说,文件的结构为:
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>
我的想法是获取每个<trade>
节点,并向其添加一个 new 子级.
这个孩子应该看起来像这样:
My idea is to get each <trade>
node and append a new child to it.
This child should look like this:
<tradeSource></tradeSource>
我的问题是,如何定义要追加的新子代?似乎我找不到在VBA上创建正确的对象(尽管该项目中已引用了MSXML v3.0库),而且我无法在网络上的任何地方找到这样的全新节点示例.我的伪代码:
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
推荐答案
这应该有效:
Set newNode= XMLFile.CreateElement("price");
newNode.InnerText = "19.95"
trade.appendChild(newNode)
请注意,变量trade
和newNode
都应声明为IXMLDOMNode
(在库msxml6.dll
中定义的类型).
Please note that both variables trade
and newNode
should be declared as IXMLDOMNode
(type defined in the library msxml6.dll
).
这篇关于创建全新的XML节点作为子节点附加到其他节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!