简体   繁体   English

VBA-如何在debug.Print中的键中检索XML键

[英]VBA - How to retrieve XML keys within keys in debug.Print

XML XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EmpDetails>
    <Employee>
      <Name>ABC</Name>
      <Dept>
        <Software1> VBA </Software1> 
        <Software2> Windows </Software2>
      </Dept> 
      <Location>New Delhi</Location>
    </Employee>
    <Employee>
      <Name>XYZ</Name>
      <Dept>
        <Software1> VBA </Software1> 
        <Software2> Windows </Software2>
      </Dept> 
      <Location>Chennai</Location>
    </Employee>
    <Employee>
      <Name>IJK</Name>
        <Dept>
          <Software1> VBA </Software1> 
          <Software2> Windows </Software2>
        </Dept> 
        <Location>Bangalore</Location>
    </Employee>
</EmpDetails>

VBA VBA

Sub Test()

Dim userBeanList As MSXML2.IXMLDOMNodeList
Dim userbean As MSXML2.IXMLDOMNode
Dim beanChild As MSXML2.IXMLDOMNode

Set xDoc = New MSXML2.DOMDocument
xDoc.Load ("C:\data\xml.xml")

Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee")

For Each userbean In userBeanList
    For Each beanChild In userbean.ChildNodes
        Debug.Print beanChild.nodeName & ":" & beanChild.Text


    Next beanChild
Next userbean

End Sub

My code currently prints from the xml, the following: 我的代码当前从xml打印,如下所示:

Name:ABC
Dept:VBA   Windows
Location:New Delhi
Name:XYZ
Dept:VBA   Windows
Location:Chennai
Name:IJK
Dept:VBA   Windows
Location:Bangalore

I want it to actually print 我要它实际打印

Name:ABC
Dept:VBA   Windows
Location:New Delhi
Name:XYZ
Software1:VBA   
Software2:Windows
Location:Chennai
Name:IJK
Software1: VBA
Software2: Windows
Location:Bangalore

In my first Example, I simply added another level (grandChild) to your loop. 在我的第一个示例中,我仅向循环添加了另一个级别(grandChild)。

Sub Example()

    Dim userBeanList As MSXML2.IXMLDOMNodeList
    Dim userbean As MSXML2.IXMLDOMNode
    Dim beanChild As MSXML2.IXMLDOMNode
    Dim grandChild As MSXML2.IXMLDOMNode

    Set xDoc = New MSXML2.DOMDocument
    xDoc.Load ("C:\Users\best buy\Desktop\xml.xml")

    Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee")

    For Each userbean In userBeanList
        For Each beanChild In userbean.ChildNodes
            Debug.Print beanChild.nodeName & ":" & beanChild.Text
            For Each grandChild In beanChild.ChildNodes
                If Not Left(grandChild.nodeName, 1) = "#" Then Debug.Print grandChild.nodeName & ":" & grandChild.Text
            Next
        Next beanChild
    Next userbean

End Sub

Here is an Example of using recursion that will print all the branches and leaf of the XML without knowing the actual structure. 这是一个使用递归的示例,该示例将在不知道实际结构的情况下打印XML的所有分支和叶子。

Sub RecursiveExample()
    Dim userBeanList As MSXML2.IXMLDOMNodeList
    Set xDoc = New MSXML2.DOMDocument
    xDoc.Load ("C:\Users\best buy\Desktop\xml.xml")

    Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee")
    RecursivePrintNodes userBeanList
End Sub

Sub RecursivePrintNodes(NodeList As MSXML2.IXMLDOMNodeList)
    Dim child As MSXML2.IXMLDOMNode
    For Each child In NodeList
        If Not Left(child.nodeName, 1) = "#" Then Debug.Print child.nodeName & ":" & child.Text
        If child.ChildNodes.Length > 0 Then RecursivePrintNodes child.ChildNodes
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM