MSXML2.DOMDocument60 - 使用命名空间在 VBA 中读取 XML

MSXML2.DOMDocument60 - Reading XML in VBA with Namespace

我是 Access 编程的新手,使用 MSXML2.DOMDocument60,所以如果有任何不正确的地方,请接受我的歉意。我正在尝试解析 xml,但在使用 MSXML2.DOMDocument 加载它时遇到问题。 XML的结构如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE raml SYSTEM 'raml20.dtd'>
<raml version="2.0" xmlns="raml20.xsd">
  <cmData type="actual">
    <header>
      <log dateTime="2021-03-11T13:00:47.000Z" action="created" appInfo="ActualExporter">InternalValues are used</log>
    </header>
    <managedObject class="RETU_R" version="EQMR20A_2003_002" distName="PLMN-PLMN/MRBTS-503327/EQM_R-1/APEQM_R-1/ALD_R-1/RETU_R-1" id="136127888">
      <p name="angle">20</p>
      <list name="antBandList">
        <item>
          <p name="antBeamwidth">61</p>
          <p name="antFreqBand">1</p>
          <p name="antOperGain">185</p>
        </item>
        <item>
          <p name="antBeamwidth">60</p>
          <p name="antFreqBand">2</p>
          <p name="antOperGain">185</p>
        </item>
        <item>
          <p name="antBeamwidth">61</p>
          <p name="antFreqBand">3</p>
          <p name="antOperGain">184</p>
        </item>
      </list>
      <p name="antBearing">2800</p>
      <p name="antModel">80010825-2.1_L</p>
      <p name="antSerial">DEG3535443</p>
      <list name="antlDNList">
        <p>external</p>
      </list>
      <p name="baseStationID">45118</p>
      <p name="configDN">MRBTS-503327/EQM-1/APEQM-1/ALD-9/RETU-1</p>
      <p name="installDate">240814</p>
      <p name="installerID">CRCTL</p>
      <p name="maxAngle">60</p>
      <p name="mechanicalAngle">0</p>
      <p name="minAngle">0</p>
      <p name="operationalState">1</p>
      <p name="sectorID">3U21</p>
      <p name="subunitNumber">1</p>
    </managedObject>
    
  </cmData>
</raml>

我知道我的代码有命名空间,这可能会导致所有问题。我在 vba 中创建了错误解码消息,并收到以下错误:

"Could not load the document: C:\Audit_DB\Input Files\Test1.xml
Error when loading was: The element 'raml' is used but not declared in the DTD/Schema."

有人可以告诉我如何跳过此处的名称空间以及 vba 代码是否有任何问题。我创建的代码如下:

Sub XMLRead()

Dim path As String
Dim firstNameField As MSXML2.IXMLDOMNodeList
Dim lists As MSXML2.IXMLDOMNodeList
Dim raml As MSXML2.IXMLDOMElement

Dim i As Integer
Dim objXML As MSXML2.DOMDocument60
Set objXML = New MSXML2.DOMDocument60
path = "C:\Audit_DB\Input Files\Test1.xml"

objXML.SetProperty "ProhibitDTD", False

With objXML
    .async = False
    .Load path
    .SetProperty "SelectionLanguage", "XPath"
    .SetProperty "ProhibitDTD", False
    .SetProperty "SelectionNamespaces", "xmlns:raml='raml20.xsd'"
    Set nodeList = .selectNodes("//managedObject")
End With


If objXML.Load(path) Then
    Debug.Print "Success"
Else
    Debug.Print "Could not load the document: " & path
    If objXML.parseError.errorCode <> 0 Then Debug.Print "Error when loading was: " + objXML.parseError.reason
End If
    
Set xobjdetails = objXML.childNodes(0)
Set xObject = objXML.firstChild
    

Debug.Print objXML.selectNodes("//managedObject").length

End Sub
With objXML
    ...
    .validateOnParse = False
    ...
End With

XML 解析器正在尝试遵循文档类型声明:

<!DOCTYPE raml SYSTEM 'raml20.dtd'>

您可以提供 raml20.dtd 文件,以便 XML 解析器在加载 XML 时可以找到它,或者您可以禁用自动验证和外部解析DOMDocument 中的引用(例如 DTD)(参见 MSDN):

With objXML
    ' ...
    .resolveExternals = False
    .validateOnParse = False
    ' ...
    .load "filepath"
End With

必须禁用这两个设置,否则加载将不会成功。确保在 尝试加载文件之前 设置它们。


也就是说,//managedObject 不会找到任何内容,因为该节点位于 raml20.xsd 命名空间中,就像文档中的所有其他元素一样。

您已经将该命名空间绑定到前缀 raml(使用 .SetProperty "SelectionNamespaces", "xmlns:raml='raml20.xsd'"),但您还需要使用前缀:

Debug.Print objXML.selectNodes("//raml:managedObject").length

最后,您的 VBA 代码需要一些清理。您设置了两次 ProhibitDTD,并且您也多次调用了 .load