示例XML:
<DATA>
<DIMENSION Name="NST1">
<MEMBERS>
<MEMBER>
<LABEL>[None]</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
<MEMBER>
<LABEL>TotalNST1</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
</MEMBERS>
</DIMENSION>
<DIMENSION Name="NST2">
<MEMBERS>
<MEMBER>
<LABEL>[None]</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
<MEMBER>
<LABEL>TotalNST1</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
</MEMBERS>
</DIMENSION>
|
| MANY MANY MORE DIMENSIONS HERE
|
</DATA>
使用上面的xml文件我试图使用VBA创建一个字典,其中包含许多键,这些键对应于每个DIMENSION节点的名称属性,以及连接在一起的所有元素作为与该键相关联的条目。
IE:
Key: Data:
NST1: NST1; [None]; N; N; N;
NST2: NST2; [None]; N; N; N;
使用getElementsByTagName(DIMENSION)很简单但是我的问题是,你可以看到XML文件包含许多具有相同布局和标签名称的维度&#34; DIMENSION&#34;仅在Dimension标记中通过属性Name进行区分。
有没有办法通过&#34; name属性选择维度&#34;即NST1,NST2等而不是它的标签名称?
我的VBA脚本是&#34;查看正确的维度&#34;我有创建这些字典条目所需的代码。我的问题是&#34;看看&#34;一个维度的属性?
我们将非常感激任何想法或想法。
非常感谢
答案 0 :(得分:0)
您可以使用XPath,如下例所示:
Set objXMLDoc = CreateObject("Msxml2.DOMDocument")
objXMLDoc.async = False
objXMLDoc.load "C:\Sample.xml"
objXMLDoc.setProperty "SelectionLanguage", "XPath"
' set target value of the Name attribute NST2
strTargetName = "NST2"
' the resulting node list contains all DIMENSION nodes having NST2 as Name
Set objNodeList = objXMLDoc.documentElement.selectNodes("//DIMENSION[@Name='" & strTargetName & "']")
' show the content of the first NST2 node from collection
MsgBox objNodeList(0).xml
我将您的XML片段保存到C:\ Sample.xml中,输出如下:
<DIMENSION Name="NST2">
<MEMBERS>
<MEMBER>
<LABEL>[None]</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
<MEMBER>
<LABEL>TotalNST1</LABEL>
<AT Name="IsCalculated">N</AT>
<AT Name="SwitchSignForFlow">N</AT>
<AT Name="SwitchTypeForFlow">N</AT>
</MEMBER>
</MEMBERS>
</DIMENSION>