【问题标题】:vba and XML only need to extract attribute values from certain nodesvba 和 XML 只需要从某些节点中提取属性值
【发布时间】:2015-02-11 07:29:23
【问题描述】:

我是 XML 新手。我现在有一个要求,我必须从一堆 XML 文件中提取数据。我无法显示整个 XML 文件,但希望有人能告诉我如何做到这一点。我已经阅读了很多论坛和博客,我只是很困惑。这是 XML。

<?xml version="1.0"?>
<docpms>
  <distrib>
  <mrc>    
    <toolsetc>              
      <misc>
        <seqlist>
          <item spinno="02">
            <paratext>Gloves</paratext>
          </item>
          <item spinno="022">
            <paratext>Pail</paratext>
          </item>
          <item spinno="03">
            <paratext>Lanyard</paratext>
          </item>
          <item spinno="07">
            <paratext>Goggles</paratext>
          </item>

所以,我想遍历具有 spinno 属性的项目节点并获取该属性的文本值。还有很多其他的item节点没有这个属性,所以我想我需要使用类似下面的VBA。

Option Compare Database
Option Explicit

Dim xDoc As MSXML2.DOMDocument60
Dim strpath As String
Dim spinAttribute As MSXML2.IXMLDOMAttribute

Public Sub LoadDocument()
strpath = Environ("USERPROFILE") & "\Documents\XML\00\"
Set xDoc = New MSXML2.DOMDocument60
xDoc.validateOnParse = False
If (xDoc.Load(strpath & "1C.xml")) Then
   ' The document loaded successfully.
   ' Now do something intersting.
   DisplayNode xDoc.childNodes, 0
Else
   ' The document failed to load.
   ' See the previous listing for error information.
End If
End Sub

Public Sub DisplayNode(ByRef Nodes As MSXML2.IXMLDOMNodeList, _
   ByVal Indent As Integer)
    Dim strSpin As String
    
   Dim xNode As MSXML2.IXMLDOMNode
   Set xNode = xDoc.selectNodes("/docpms/mrc/toolsetc/misc/seqlist/item")
   

   For Each xNode In Nodes
   Set spinAttribute = xNode.Attributes.getNamedItem("spinno")
    strSpin = spinAttribute.Text
      
   Next xNode
End Sub

但是当我单步执行代码时,对于初学者,我在 Set xNode = xDoc.selectNodes("/docpms/mrc/toolsetc/misc/seqlist/item") 上遇到类型不匹配,我不确定我的 for每个循环都会做我想做的事。任何帮助将不胜感激。

谢谢,

大卫

【问题讨论】:

  • 您基本上可以使用正则表达式解析您的 XML 文件以获取您的节点内容。
  • 你的意思是使用类似 INSTR 的东西来查找节点和属性吗?我可以这样做,但我更愿意学习使用 DOM 来按照设计的方式进行操作。

标签: xml vba


【解决方案1】:

我在 Stack Overflow 上的另一个论坛帖子中找到了答案。抱歉,我今天早些时候没有看到这个。这是答案How I can read all Attributes from a XML with VBA?,这是我的最终代码。

Private Sub Workbook_Open()
  Dim spin As String
  Dim xmlUrl As String
  Dim xmlDoc As New DOMDocument
  Dim n As IXMLDOMNode
  Dim nText As String


  xmlUrl = Environ("USERPROFILE") & "\Documents\Viewer\XML\00\001C.xml"
  xmlDoc.async = False

  If Not xmlDoc.Load(xmlUrl) Then
    MsgBox "XML LOAD ERROR"
  Else

    For Each n In xmlDoc.selectNodes("//misc/seqlist/item")
      spin = n.Attributes.getNamedItem("spinno").Text
        nText = n.Text
    Next

  End If

End Sub

这会搜索并获取我需要的所有属性值。

谢谢,

大卫

【讨论】:

    【解决方案2】:

    您可以使用 XPath 仅选择具有该属性的节点:

    Dim iNodes, n
    Set xDoc = New MSXML2.DOMDocument60
    
    xDoc.validateOnParse = True
    xDoc.LoadXML Sheet1.Range("a1").Value 'my testing
    
    xDoc.setProperty "SelectionLanguage", "XPath" '<<<######
    
    'I simplified your XML a bit, so adjust here....
    Set iNodes = xDoc.SelectNodes("/docpms/seqlist/item[@spinno]")
    
    Debug.Print iNodes.Length
    For Each n In iNodes
        Debug.Print n.Text 'all itens with the spinno attribute
    Next n
    

    【讨论】:

    • 感谢蒂姆的回复。我刚刚开始使用 Xpath。我参加了 W3 学校的教程。看起来你可以用 Xpath 做很多事情。我实际上是在学校学习它,同时,我要求我在工作中使用它。哇!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    相关资源
    最近更新 更多