在 VBA Excel 的 XML 阶段遇到问题

Having trouble with XML phase on VBA Excel

我需要解析一个 XML 结构如下的文件:(我无法显示数据,因为它是机密数据)

<?xml version="1.0" encoding="UTF-8"?>
<GACDWBulkLoadInterface xsi:schemaLocation="http://www.example.org/GACDWSchema GACDWSchema.xsd" xmlns="http://www.example.org/GACDWSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <BLConfiguration>
          <BLProperties>
             <BLProperty>
                <key>isEmpty</key>
                <value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:boolean">true</value>
             </BLProperty>
          </BLProperties>
          <version>api-ag-1.1.68-20210315-43</version>
       </BLConfiguration>
       <Customer>
          <CustomerID></CustomerID>
          <Customer_Name></Customer_Name>
          <ISOCountryCode></ISOCountryCode>
          <customerType></customerType>
          <dataSourceProvider></dataSourceProvider>
          <function></function>
          <TopLevelCI>
             <Contact Action="Create">
                <Id>4</Id>
                <artifactType></artifactType>
                <contactType></contactType>
                <email></email>
                <firstName></firstName>
                <lastName></lastName>
                <userID></userID>
                <ChildCI/>
             </Contact>
             <PhysicalComputerSystem Action="Create">
                <Id>2</Id>
                <artifactType></artifactType>
                <assetLifeCycleState></assetLifeCycleState>
                <category></category>
                <ciLifeCycleState></ciLifeCycleState>
                <custname></custname>
                <description></description>
                <discontinuedDate></discontinuedDate>
                <machineType></machineType>
                <managed></managed>
                <manufacturer></manufacturer>
                <model></model>
                <serialNumber></serialNumber>
                <virtualFlag></virtualFlag>
                <ChildCI>
                   <PhysicalComputerSystemContact Action="Create">
                      <contactUID></contactUID>
                   </PhysicalComputerSystemContact>
                   <PhysicalComputerSystemLocation Action="Create">
                      <buildingID></buildingID>
                      <buildingName></buildingName>
                      <city></city>
                      <countryName></countryName>
                      <isoCountryCode></isoCountryCode>
                      <postalCode></postalCode>
                      <siteName></siteName>
                      <stateProvince></stateProvince>
                      <streetAddress></streetAddress>
                   </PhysicalComputerSystemLocation>
                </ChildCI>
             </PhysicalComputerSystem>
             <OperatingSystem Action="Create">
                <Id>1</Id>
                <aliasHostname></aliasHostname>
                <artifactType></artifactType>
                <assetLifeCycleState></assetLifeCycleState>
                <backupContr></backupContr>
                <barSaId></barSaId>
                <cacfCustomGroup></cacfCustomGroup>
                <category></category>
                <ciLifeCycleState></ciLifeCycleState>
                <classification></classification>
                <cpuCount></cpuCount>
                <description></description>
                <discontinuedDate></discontinuedDate>
                <fqhn></fqhn>
                <hostsystemID></hostsystemID>
                <hostsystemType></hostsystemType>
                <installationDate></installationDate>
                <ipAddress></ipAddress>
                <majorBusProc></majorBusProc>
                <managed></managed>
                <memory></memory>
                <memoryUnit></memoryUnit>
                <osModLevel></osModLevel>
                <osName></osName>
                <osBuildNumber></osBuildNumber>
                <osProvider></osProvider>
                <osRelease></osRelease>
                <osVersion></osVersion>
                <prodDate></prodDate>
                <purgeDate></purgeDate>
                <purpose></purpose>
                <securityClass></securityClass>
                <serverType></serverType>
                <supportEnddate></supportEnddate>
                <useAliasHostname></useAliasHostname>
                <virtualFlag></virtualFlag>
                <ChildCI>
                   <OperatingSystemContact Action="Create">
                      <contactUID></contactUID>
                   </OperatingSystemContact>
                   <OperatingSystemEnvironmentProfile Action="Create">
                      <authFlag></authFlag>
                      <authExceptDate></authExceptDate>
                      <authExceptReasonCode></authExceptReasonCode>
                      <authExceptText></authExceptText>
                      <disasterRecSys></disasterRecSys>
                      <domain></domain>
                      <extUsersMax></extUsersMax>
                      <hcAutoInterv></hcAutoInterv>
                      <hcExceptDate></hcExceptDate>
                      <hcExceptReasonCode></hcExceptReasonCode>
                      <hcExceptText></hcExceptText>
                      <hcUnit></hcUnit>
                      <healthchAutoFlag></healthchAutoFlag>
                      <highestClientDataClassification></highestClientDataClassification>
                      <highestDataClassification></highestDataClassification>
                      <highestIbmDataClassification></highestIbmDataClassification>
                      <ibmCbnInterval></ibmCbnInterval>
                      <ibmCbnRequired></ibmCbnRequired>
                      <ibmCbnType></ibmCbnType>
                      <ibmCbnUnit></ibmCbnUnit>
                      <ibmPrivIDUnit></ibmPrivIDUnit>
                      <ibmPrivType></ibmPrivType>
                      <ibmQevFlag></ibmQevFlag>
                      <ibmQevInterval></ibmQevInterval>
                      <ibmQevUnit></ibmQevUnit>
                      <ibmUsersMax></ibmUsersMax>
                      <internetAccFlag></internetAccFlag>
                      <personalData></personalData>
                      <privIDFlag></privIDFlag>
                      <privIDInterv></privIDInterv>
                      <regulatoryRequirements></regulatoryRequirements>
                      <remediationtimeHi></remediationtimeHi>
                      <remediationtimeLo></remediationtimeLo>
                      <remediationtimeMd></remediationtimeMd>
                      <rerFlag></rerFlag>
                      <rerInterv></rerInterv>
                      <rerType></rerType>
                      <rerUnit></rerUnit>
                      <scExceptDate></scExceptDate>
                      <scExceptReasonCode></scExceptReasonCode>
                      <scExceptText></scExceptText>
                      <secExemptionDesc></secExemptionDesc>
                      <secExemptionFlag></secExemptionFlag>
                      <securityPackage></securityPackage>
                      <securityPolicyCategory></securityPolicyCategory>
                      <securityPolicyOther></securityPolicyOther>
                      <securityPolicyType></securityPolicyType>
                      <serverPurpose></serverPurpose>
                      <sharedIDFlag></sharedIDFlag>
                      <shrdExceptDate></shrdExceptDate>
                      <shrdExceptReasonCode></shrdExceptReasonCode>
                      <shrdExceptText></shrdExceptText>
                      <vitalBusProcessYN></vitalBusProcessYN>
                   </OperatingSystemEnvironmentProfile>
                   <OperatingSystemIpAddress Action="Create">
                      <dotnotation></dotnotation>
                      <hostName></hostName>
                      <isPrimaryIpHost></isPrimaryIpHost>
                      <name></name>
                      <netMask></netMask>
                      <vsExceptDate></vsExceptDate>
                      <vsExceptReasonCode></vsExceptReasonCode>
                      <vsExceptText></vsExceptText>
                      <vulScanUnit></vulScanUnit>
                      <vulscanAutoFlag></vulscanAutoFlag>
                      <vulscanAutoInterval></vulscanAutoInterval>
                   </OperatingSystemIpAddress>
                </ChildCI>
             </OperatingSystem>
             <Subsystem Action="Create">
                <Id>3</Id>
                <applSIDataType></applSIDataType>
                <artifactType></artifactType>
                <assetLifeCycleState></assetLifeCycleState>
                <ciLifeCycleState></ciLifeCycleState>
                <controlProductName></controlProductName>
                <controlProductRelease></controlProductRelease>
                <controlProductType></controlProductType>
                <controlProductVendor></controlProductVendor>
                <controlProductVersion></controlProductVersion>
                <description></description>
                <discontinuedDate></discontinuedDate>
                <fqhn></fqhn>
                <managed></managed>
                <name></name>
                <parentUID></parentUID>
                <piFlag></piFlag>
                <productModLevel></productModLevel>
                <productName></productName>
                <productPatchLevel></productPatchLevel>
                <productRelease></productRelease>
                <productVersion></productVersion>
                <purgeDate></purgeDate>
                <scanTime></scanTime>
                <vendorName></vendorName>
                <ChildCI>
                   <SubsystemContact Action="Create">
                      <contactUID></contactUID>
                   </SubsystemContact>
                   <SubsystemEnvironmentProfile Action="Create">
                      <authFlag></authFlag>
                      <authExceptDate></authExceptDate>
                      <authExceptReasonCode></authExceptReasonCode>
                      <authExceptText></authExceptText>
                      <disasterRecSys></disasterRecSys>
                      <hcAutoInterv></hcAutoInterv>
                      <hcExceptDate></hcExceptDate>
                      <hcExceptReasonCode></hcExceptReasonCode>
                      <hcExceptText></hcExceptText>
                      <hcUnit></hcUnit>
                      <healthchAutoFlag></healthchAutoFlag>
                      <ibmCbnInterval></ibmCbnInterval>
                      <ibmCbnRequired></ibmCbnRequired>
                      <ibmCbnType></ibmCbnType>
                      <ibmCbnUnit></ibmCbnUnit>
                      <ibmPrivIDUnit></ibmPrivIDUnit>
                      <ibmPrivType></ibmPrivType>
                      <ibmQevFlag></ibmQevFlag>
                      <ibmQevInterval></ibmQevInterval>
                      <ibmQevUnit></ibmQevUnit>
                      <privIDFlag></privIDFlag>
                      <privIDInterv></privIDInterv>
                      <rerFlag></rerFlag>
                      <rerInterv></rerInterv>
                      <rerType></rerType>
                      <rerUnit></rerUnit>
                      <scExceptDate></scExceptDate>
                      <scExceptReasonCode></scExceptReasonCode>
                      <scExceptText></scExceptText>
                      <sharedIDFlag></sharedIDFlag>
                      <shrdExceptDate></shrdExceptDate>
                      <shrdExceptReasonCode></shrdExceptReasonCode>
                      <shrdExceptText></shrdExceptText>
                      <vitalBusProcessYN></vitalBusProcessYN>
                   </SubsystemEnvironmentProfile>
                </ChildCI>
             </Subsystem>
          </TopLevelCI>
          <Relationship>
             <RelationshipName>Hosted_by</RelationshipName>
             <sourceCIType>OperatingSystem</sourceCIType>
             <targetCIType>OperatingSystem</targetCIType>
          </Relationship>
          <Relationship>
             <RelationshipName>Runs_on</RelationshipName>
             <sourceCIType>Subsystem</sourceCIType>
             <targetCIType>OperatingSystem</targetCIType>
          </Relationship>
          <Relationship>
             <RelationshipName>Runs_on_hw</RelationshipName>
             <sourceCIType>OperatingSystem</sourceCIType>
             <targetCIType>PhysicalComputerSystem</targetCIType>
          </Relationship>
       </Customer>
</GACDWBulkLoadInterface>

我在这里研究并提出了我的 VBA 代码

Sub ReadXML()
    Dim oXML As MSXML2.DOMDocument60
    Dim vaFile As Variant
    
    Set oXML = New MSXML2.DOMDocument60
    
    'Open Browse file dialog
    vaFile = Application.GetOpenFilename("XML Files (*.xml), *.xml", _
                  Title:="Select XML files", MultiSelect:=False)
    oXML.validateOnParse = True
    oXML.setProperty "SelectionLanguage", "XPath"   ' necessary in version 3.0, possibly redundant here
    oXML.async = False
        
        
         If Not oXML.Load(vaFile) Then 'Load XML has gone bad
            Dim xPE        As Object    ' Set xPE = CreateObject("MSXML2.IXMLDOMParseError")
            Dim strErrText As String
            Set xPE = oXML.parseError
            With xPE
               strErrText = "Load error " & .ErrorCode & " xml file " & vbCrLf & _
               Replace(.URL, "file:///", "") & vbCrLf & vbCrLf & _
              xPE.reason & _
              "Source Text: " & .srcText & vbCrLf & vbCrLf & _
              "Line No.:    " & .Line & vbCrLf & _
              "Line Pos.: " & .linepos & vbCrLf & _
              "File Pos.:  " & .filepos & vbCrLf & vbCrLf
            End With
            MsgBox strErrText, vbExclamation
            Set xPE = Nothing
            Exit Sub
        End If
        
    Debug.Print "|" & oXML.XML & "|"
    
    Dim nodeList  As MSXML2.IXMLDOMNodeList, iNode As MSXML2.IXMLDOMNode
    Dim Searched As String
    Searched = "/*/*"
    
    Set nodeList = oXML.SelectNodes(Searched)
    
    'Set Queries = oXML.DocumentElement.SelectNodes(Searched)
    
    For Each iNode In nodeList
         'Debug.Print "<" & iNode.BaseName & ">"
         Debug.Print "<" & iNode.nodeName & ">"
         
    Next

但是,无论我提出什么 Xpath 查询,我的问题都是。 None 除了 \*\. 工作 我无法指定任何路径。 .SelectSingleNode.SelectNodes 总是 return 长度 =0。我的代码或我的 XML 文件或 Xpath 语法有问题吗?

如评论中所述,您的 xml 文档在其 DocumentElement <GACDWBulkLoadInterface> 中具有 namespace 定义(xmlns 代表 xmln相同s速度)。此外 "它包含一个 默认命名空间 因此任何在命名节点上尝试的解析都必须映射到这个命名空间 URI 否则 returns 没什么

为了进行最终分析,有必要将用户定义的 prefix(例如 :s)包含到显式 命名空间设置,可以在后面的XPath表达式中使用:

    Dim oXML   As MSXML2.DOMDocument60
    Set oXML = New MSXML2.DOMDocument60
    oXML.validateOnParse = True

    Dim XMLNamespaces As String
    XMLNamespaces = "xmlns:s='http://www.example.org/GACDWSchema'"
    oXML.SetProperty "SelectionNamespaces", XMLNamespaces

最终您可以定义任何 XPath 表达式,例如Customer[1] 的子节点:

    Dim nodeList As MSXML2.IXMLDOMNodeList, iNode As MSXML2.IXMLDOMNode
    Dim Searched As String
    Searched = "//s:Customer/s:*"

    Set nodeList = oXML.SelectNodes(Searched)

相关链接

  • Displaying XML structures including attributes via recursive function calls