使用VBA和xPath遍历XML文件

5
我在我的MS Project VBA代码中使用xPath解析/提取XML时遇到了困难。
为什么我不能选择这个节点中的一个节点?
Set nodes = xml.SelectNodes("/config/ProjectFile")
For Each node In nodes
    With Me.lbProjList
      '.AddItem (xmlText(node.SelectSingleNode("/FileName")))
      '.Column(1, i) = xmlText(node.SelectSingleNode("/LastSaveDate"))
    End With
    i = i + 1
    Debug.Print i & " file " & node.xml ' Shows the XML I expected
    Debug.Print "  Name: " & node.SelectSingleNode("/FileName").Text ' Doesn't work! Error 91
Next node

我很乐意提供帮助!

谢谢!


以下是完整的VBA代码:

Private Sub ProjListFill()
  Dim i As Integer
  Dim xml As MSXML2.DOMDocument60
  Dim nodes As MSXML2.IXMLDOMNodeList 'CustomXMLNodes???
  Dim node As MSXML2.IXMLDOMNode 'CustomXMLNode???
  Dim n As CustomXMLPart 'CustomXMLNode???

  ' clear form before fill it
  Me.lbProjList.Clear
  Me.txtHeadline.value = ""
  Me.txtUpdateURL.value = ""
  Me.txtBoxParam.value = ""
  Me.txtBoxPrefix.value = ""

  Set xml = readXML(CustomProperty("XMTMLMonitoring_AppPath") & "\" & m2w_config("SubFolder") & "\" & m2w_config("SubFolderData") & "\" & m2w_config("XMLConfigFileName"))

  i = 0
  Set nodes = xml.SelectNodes("/config/ProjectFile")
  For Each node In nodes
    With Me.lbProjList
      '.AddItem (xmlText(node.SelectSingleNode("/FileName")))
      '.Column(1, i) = xmlText(node.SelectSingleNode("/LastSaveDate"))
    End With
    i = i + 1
    Debug.Print i & " file " & node.xml ' Shows the XML I expected        Debug.Print "  Name: " & node.SelectSingleNode("/FileName").Text ' Doesn't work! Error 91
  Next node

  Debug.Print i & " Project files found in config.xml"

  ' fill text boxes
  Me.txtHeadline.value = xmlText(xml.SelectSingleNode("/config/Custom/Headline"))
  Me.txtUpdateURL.value = xmlText(xml.SelectSingleNode("/config/Custom/UpdateURL"))
  Me.txtBoxParam.value = xmlText(xml.SelectSingleNode("/config/Custom/BoxParam"))
  Me.txtBoxPrefix.value = xmlText(xml.SelectSingleNode("/config/Custom/BoxPrefix"))

ExitProjListFill:
  Exit Sub
End Sub

以下是XML代码:

<config id="config" ConfigSaveDate="2011-03-31 21:32:55" ConfigSchemaVersion="1.02">
    <Custom> 
        <DateFormat>yyyy-mm-dd hh:mm:ss</DateFormat>
        <Headline>Project Overview</Headline>
        <UpdateHref></UpdateHref>
        <BoxParam>ModelYear</BoxParam><BoxPrefix>MY </BoxPrefix>
    </Custom>
    <Program>
        <DateFormat>yyyy-mm-dd hh:mm:ss</DateFormat> 
    </Program>
    <ProjectFile ProjectFileName="projectfile1.mpp">
        <RevisionNumber>201</RevisionNumber> 
        <FileName>projectfile1.mpp</FileName> 
        <LastSaveDate>2011-03-23 16:45:19</LastSaveDate> 
    </ProjectFile>
    <ProjectFile ProjectFileName="projectfile2bedeleted.mpp">
        <RevisionNumber>115</RevisionNumber> 
        <FileName>projectfile2b.mpp</FileName> 
        <LastSaveDate>2011-03-31 21:12:55</LastSaveDate> 
    </ProjectFile>
    <ProjectFile ProjectFileName="projectfile2.mpp">
        <RevisionNumber>315</RevisionNumber> 
        <FileName>projectfile3.mpp</FileName> 
        <LastSaveDate>2011-03-31 21:32:55</LastSaveDate> 
    </ProjectFile>
</config>
2个回答

9

不要使用绝对路径,

node.SelectSingleNode("/FileName").Text

尝试使用相对路径(不包含/):

node.SelectSingleNode("FileName").Text

免责声明:由于您没有向我们展示XML文件的样本,因此这主要是推测...

我添加了XML。猜猜看,“node.SelectSingleNode("FileName").Text”起作用了!!!谢谢! - BBQ Chef

1
我需要进行一些解析工作,在几个小时后,我意识到使用单个XPath获取数据可能会变得困难。
可能有许多原因:
1.糟糕的XML结构 2.重复标签 3.等等
我使用了XPath和内置函数的组合来获取我的数据。例如:
strFile = "C:\MyFile.xml"
intFile = 2
Open strFile For Input As intFile

'Load XML into string strXML
While Not EOF(intFile)
    Line Input #intFile, strXML
Wend
Close intFile

Dim XMLDOC As MSXML2.DOMDocument
Set objDOM = CreateObject("Msxml2.DOMDocument.6.0")
Dim xmlNodes As MSXML2.IXMLDOMNodeList
objDOM.LoadXML strXML
XPath = "/query/results"
Set xmlNode = objDOM.SelectNodes(XPath)
rowCounter = WorksheetFunction.CountA(output.Columns("A")) + 1
Set oNodes = objDOM.getElementsByTagName("quote")
If oNodes.Length > 0 Then
    For Each oNode In oNodes
        output.Cells(rowCounter, 1) = symbol
        output.Cells(rowCounter, 2) = oNode.SelectSingleNode("Date").Text
        output.Cells(rowCounter, 3) = oNode.SelectSingleNode("Open").Text
        output.Cells(rowCounter, 4) = oNode.SelectSingleNode("High").Text
        output.Cells(rowCounter, 5) = oNode.SelectSingleNode("Low").Text
        output.Cells(rowCounter, 6) = oNode.SelectSingleNode("Close").Text
        output.Cells(rowCounter, 7) = oNode.SelectSingleNode("Volume").Text
        output.Cells(rowCounter, 8) = oNode.SelectSingleNode("Adj_Close").Text
        rowCounter = rowCounter + 1
    Next oNode
End If

您可以在我的博客中找到Excel表格。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接