在VBA中进行XML解析期间,如何显示子节点的父节点

时间:2018-08-17 10:35:13

标签: xml excel vba excel-vba

我有一个要在VBA中解析的XML,如下所示:

<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>

如您所见,PEFP和PEDP的每个子节点共享相同的节点名称。

这是我正在使用的代码:

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

这就是我得到的:

UniqueIdentifier
Surname
FirstName
CountryText
PEOrganizationText

UniqueIdentifier
Surname
FirstName
CountryText
PEOrganizationText

有什么方法可以添加父节点来区分子节点?

2 个答案:

答案 0 :(得分:2)

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

myNode.ParentNode.ParentNode.BaseName

答案 1 :(得分:1)

您已经给出了答案。我将补充说,在解析时验证对警告您加载无效的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