VBA 循环遍历 XML 节点并在每个节点中获取值
Posted
技术标签:
【中文标题】VBA 循环遍历 XML 节点并在每个节点中获取值【英文标题】:VBA loop through XML nodes and get values within each node 【发布时间】:2021-06-22 15:12:58 【问题描述】:在 Access 中,我有一个表单,它将加载用户提供的 XML 文件,然后循环从文件中提取信息。这是 XML 文件的结构。
<manifest identifier="MANIFEST1">
<organizations />
<resources>
<resource identifier="AMS_EDDC7208" type="imsqti_xmlv1p2p1" href="a1231.xml">
<metadata>
<schema>abc123</schema>
<schemaversion>1.2</schemaversion>
<metadata>
<exmetadata>
<exmetadataSection identifier="841" name="Topic">
<description>
</description>
<exmetadataparameterSet>
<param name="Topic" type="String">Math</param>
<param name="Gender" type="String">N</param>
<param name="Race" type="String">Z</param>
<param name="Difficulty Level" type="String">HARD</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
<exmetadataSection identifier="842" name="Math Reasoning">
<description>
</description>
<exmetadataparameterSet>
<param name="Item_Type" type="String">A</param>
<param name="Cont1" type="String">ARI</param>
<param name="Cont2" type="String">REM</param>
<param name="Cognitive" type="String">PUR</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
</exmetadata>
</metadata>
</metadata>
<file href="a1231.xml" />
</resource>
<resources>
<resource identifier="AMS_AB3dC7208" type="imsqti_xmlv1p2p1" href="a1232.xml">
<metadata>
<schema>abc123</schema>
<schemaversion>1.2</schemaversion>
<metadata>
<exmetadata>
<exmetadataSection identifier="841" name="Topic">
<description>
</description>
<exmetadataparameterSet>
<param name="Topic" type="String">Math</param>
<param name="Gender" type="String">N</param>
<param name="Race" type="String">Z</param>
<param name="Difficulty Level" type="String">HARD</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
<exmetadataSection identifier="842" name="Math Reasoning">
<description>
</description>
<exmetadataparameterSet>
<param name="Item_Type" type="String">A</param>
<param name="Cont1" type="String">GEO</param>
<param name="Cont2" type="String">ABS</param>
<param name="Cognitive" type="String">PUR</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
</exmetadata>
</metadata>
</metadata>
<file href="a1232.xml" />
</resource>
<resources>
<resource identifier="AMS_EE0023DC7208" type="imsqti_xmlv1p2p1" href="a1233.xml">
<metadata>
<schema>abc123</schema>
<schemaversion>1.2</schemaversion>
</metadata>
<file href="a1233.xml" />
</resource>
<resources>
<resource identifier="AMS_XXZDF2323CC208" type="imsqti_xmlv1p2p1" href="a1234.xml">
<metadata>
<schema>abc123</schema>
<schemaversion>1.2</schemaversion>
<metadata>
<exmetadata>
<exmetadataSection identifier="841" name="Topic">
<description>
</description>
<exmetadataparameterSet>
<param name="Topic" type="String">Math</param>
<param name="Gender" type="String">N</param>
<param name="Race" type="String">Z</param>
<param name="Difficulty Level" type="String">HARD</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
<exmetadataSection identifier="842" name="Math Reasoning">
<description>
</description>
<exmetadataparameterSet>
<param name="Item_Type" type="String">A</param>
<param name="Cont1" type="String">ALG</param>
<param name="Cont2" type="String">APP</param>
<param name="Cognitive" type="String">REA</param>
</exmetadataparameterSet>
<exdependencysection identifierref="" />
</exmetadataSection>
</exmetadata>
</metadata>
</metadata>
<file href="a1234.xml" />
</resource>
</manifest>
我能够遍历 resource 节点并获取每个节点的 identifer 和 href 文本,但我遇到了问题当我遍历每个资源节点时,从 param 节点获取正确的详细信息。我只需要某些参数值,因此尝试使用 SelectSingleNode 但意识到这只会给我它遇到的第一个集合。
Dim XDoc, nodesThatMatter As Object
Dim strFieldName, strESID, strFile, strTopic, strItemType, strCont1, strCont2, strCognitive As String
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (Me.FileIn)
Set nodesThatMatter = XDoc.SelectNodes("//resource")
For Each node In nodesThatMatter
'resource identifier
strESID = node.Attributes.Item(0).Text
'resource file
strFile = node.Attributes.Item(2).Text
strTopic = node.SelectSingleNode("//param[@name='Topic']").Text
strItemType = node.SelectSingleNode("//param[@name='Item_Type']").Text
strCont1 = node.SelectSingleNode("//param[@name='Cont1']").Text
strCont2 = node.SelectSingleNode("//param[@name='Cont2']").Text
strCognitive = node.SelectSingleNode("//param[@name='Cognitive']").Text
Next node
Set XDoc = Nothing
如何根据我所在的资源节点更新这些值,并考虑节点何时可能没有参数节点?
【问题讨论】:
【参考方案1】:我最终深入到子节点并能够得到我需要的东西。仍然想知道是否有更直接的方法可以通过名称直接进入特定级别?
【讨论】:
以上是关于VBA 循环遍历 XML 节点并在每个节点中获取值的主要内容,如果未能解决你的问题,请参考以下文章