从 Excel VBA 创建 xml 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5257258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating xml attributes from Excel VBA
提问by Adrian
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0. So far it works fine, except for some attributes. This is an excerpt from my code:
我正在尝试使用 Microsoft XML 6.0 从 Excel VBA 编写一个 xml 文件。到目前为止,它工作正常,除了一些属性。这是我的代码的摘录:
Dim block,knoten,subknoten, subknoten2 As MSXML2.IXMLDOMNode
Dim attribut, attribut2 As MSXML2.IXMLDOMAttribute
'...'
Set knoten = block.appendChild(.createNode(NODE_ELEMENT, "name", ""))
Set attribut = .createAttribute("id")
attribut.nodeValue = "Knotentext"
knoten.setAttributeNode attribut ' works fine so far '
Set subknoten = knoten.appendChild(.createNode(NODE_ELEMENT, "unterknoten", ""))
Set subknoten2 = subknoten.appendChild(.createNode(NODE_ELEMENT, "unterknoten2", ""))
subknoten2.nodeTypedValue = "Knotentext"
Set attribut = .createAttribute("id")
attribut.Value ="Attributstext"
subknoten2.setAttributeNode attribut ' this line creates an error, rest is ok
'...'
The last line leads to the Compiler message "Method or object not found" This fits with the fact that "setAttributeNode" is not in the selection list when entering subknoten2. But what's the difference to knoten? They have both been defined and created the same way. In the monitoring window (überwachungsfenster) I see the following types: knoten: Variant/Object/IXMLDOMElement subknoten2: IXMLDOMNODE/IXMLDOMElement
最后一行导致编译器消息“未找到方法或对象”这与进入 subknoten2 时“setAttributeNode”不在选择列表中的事实相符。但是打结有什么区别呢?它们都以相同的方式定义和创建。在监控窗口 (überwachungsfenster) 中,我看到以下类型:knoten: Variant/Object/IXMLDOMElement subknoten2: IXMLDOMNODE/IXMLDOMElement
Does anyone have an idea what's going on here and how I can attach an attribute to subknoten2? Thanks for your time...
有没有人知道这里发生了什么以及我如何将属性附加到 subknoten2?谢谢你的时间...
采纳答案by chris neilsen
the problem may be in the DIM declarations:
when you dim without a as clause the variable is declared as a variant
问题可能出在 DIM 声明中:
当你在没有 as 子句的情况下变暗时,变量被声明为一个变体
Dim block,knoten,subknoten, subknoten2 As MSXML2.IXMLDOMNode
Dim attribut, attribut2 As MSXML2.IXMLDOMAttribute
block,knoten,subknoten,attribut
are all variants
block,knoten,subknoten,attribut
都是变种
This explains the difference you see in the monitor window, but won't solve the underlying problem.
这解释了您在监视器窗口中看到的差异,但不能解决根本问题。
The reason you get an error is that setAttributeNode
is a method of IXMLDOMElement
, not IXMLDOMNode
您收到错误的原因是,这setAttributeNode
是一种方法IXMLDOMElement
,而不是IXMLDOMNode
Hard to be sure form the snippet posted, but it may be you need to use .createElement
rather than .createNode
很难确定发布的片段,但您可能需要使用.createElement
而不是.createNode
回答by Srinivas Rao
You can add attributes to an existing node using the following code
您可以使用以下代码向现有节点添加属性
Dim pairList As IXMLDOMNodeList
Dim pairNode As IXMLDOMNode
Dim objAttr As IXMLDOMAttribute
Set pairList = objDom.selectNodes("/PairList/*")
For Each pairNode In pairList
'--------------------------------------------------
' Setting Target Positions
'-------------------------------------------------
Set objAttr = objDom.createAttribute("TX")
pairNode.Attributes.setNamedItem(objAttr).Text = tPoint.X
Set objAttr = objDom.createAttribute("TY")
pairNode.Attributes.setNamedItem(objAttr).Text = tPoint.Y
Set objAttr = objDom.createAttribute("TZ")
pairNode.Attributes.setNamedItem(objAttr).Text = tPoint.Z
Next pairNode