简体   繁体   中英

During XML Parsing in VBA, how to display Parent Node of a Child Node

I have a XML that I am parsing in VBA and it looks like below:

<PEFPDetails>
  <PEFP>
    <UniqueIdentifier>sfdkfsd</UniqueIdentifier>
    <Surname>A1</Surname>
    <FirstName>F1</FirstName>
    <CountryText>Canada</CountryText>
    <PEOrganizationText>Bank</PEOrganizationText>
  </PEFP>
</PEFPDetails>
<PEDPDetails>
  <PEDP>
    <UniqueIdentifier>afdsfdsg</UniqueIdentifier>
    <Surname>fdsf</Surname>
    <FirstName>wqepwe</FirstName>
    <CountryText>Canada</CountryText>
    <PEOrganizationText>msdlkfds</PEOrganizationText>
  </PEDP>
</PEDPDetails>

As you can see, every child node of PEFP and PEDP share the same node-name.

This is the code that I am using:

Sub IterateThruElements()
    Dim xmlDoc      As MSXML2.DOMDocument60
    Dim xmlNodeList As MSXML2.IXMLDOMNodeList
    Dim xmlNode     As MSXML2.IXMLDOMNode
    Dim myNode      As MSXML2.IXMLDOMNode

    ' Create an Instance of the DOMDocument
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False

    ' Load XML information from a file. In this case, Cell C3 of Sheet "XML & DB2 Data" should contain the path of the XML
    strPathToXMLFile = Sheets("Credentials").Range("C3").Value
    xmlDoc.Load (strPathToXMLFile)

    ' Find out the number of child nodes in the file
    Set xmlNodeList = xmlDoc.getElementsByTagName("*")

    ' Open a new workbook and paste the data
    Sheets.Add
    ActiveSheet.Name = "TempSheet_1"
    Range("A1:B1").Formula = Array("Element Name", "Text")

    For Each xmlNode In xmlNodeList

        For Each myNode In xmlNode.ChildNodes
            If myNode.NodeType = NODE_TEXT Then
                ActiveCell.Offset(0, 0).Formula = xmlNode.nodeName
                ActiveCell.Offset(0, 1).Formula = xmlNode.Text
            End If
        Next myNode

        ActiveCell.Offset(1, 0).Select

    Next xmlNode
End Sub

This is what I am getting:

UniqueIdentifier
Surname
FirstName
CountryText
PEOrganizationText

UniqueIdentifier
Surname
FirstName
CountryText
PEOrganizationText

Is there any way by which I can add Parent Node to distinguish the child nodes?

假设您的XML结构保持不变,那么您要查找的节点是从Text节点“向上”的两个级别,因此:

myNode.ParentNode.ParentNode.BaseName

You have the answer given already. I will add that validate on parse is useful to warn you of loading invalid XML.

Option Explicit
Sub IterateThruElements()
    Dim xmlDoc      As MSXML2.DOMDocument60
    Dim xmlNodeList As MSXML2.IXMLDOMNodeList
    Dim xmlNode     As MSXML2.IXMLDOMNode
    Dim myNode      As MSXML2.IXMLDOMNode

    ' Create an Instance of the DOMDocument
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.async = False

    Dim strPathToXMLFile As String
    ' Load XML information from a file. In this case, Cell C3 of Sheet "XML & DB2 Data" should contain the path of the XML
    strPathToXMLFile = Sheets("Credentials").Range("C3").Value
    xmlDoc.validateOnParse = True

    If Not xmlDoc.Load(strPathToXMLFile) Then 
        MsgBox "Problem"
        Exit Sub
    End If

    ' Find out the number of child nodes in the file
    Set xmlNodeList = xmlDoc.getElementsByTagName("*")

    ' Open a new workbook and paste the data
    Sheets.Add
    ActiveSheet.Name = "TempSheet_1"
    Range("A1:B1").Formula = Array("Element Name", "Text")

    For Each xmlNode In xmlNodeList
        For Each myNode In xmlNode.ChildNodes
            If myNode.NodeType = NODE_TEXT Then
                ActiveCell.Offset(0, 0).Formula = myNode.ParentNode.ParentNode.BaseName & ":" & xmlNode.nodeName
                ActiveCell.Offset(0, 1).Formula = xmlNode.Text
            End If
        Next myNode

        ActiveCell.Offset(1, 0).Select
    Next xmlNode
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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