parsing从Microsoft Excel导出的XML电子表格文件

我正在尝试使用VBScript来parsing从Microsoft Excel导出的XML电子表格文件。 我开始试图计数<Row>元素的数量。 但是脚本总是返回0.我做错了什么?

这是我的VBScript文件:

 Set oXML = CreateObject("Microsoft.XMLDOM") oXML.aSync = false oXML.SetProperty "SelectionLanguage", "XPath" oXML.SetProperty "ServerHTTPRequest", True oXML.validateOnParse = False oXML.resolveExternals = False oXML.Load "_test_.xml" MsgBox oXML.SelectNodes("//Row").length ' Return 0 WScript.Quit ' Looping through all nodes works fine Set nodes = oXML.selectNodes("//*")  For i = 0 to nodes.length -1  Msgbox nodes(i).nodeName Next 

这里是XML文件:

 <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Version>14.00</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <AllowPNG/> </OfficeDocumentSettings> <Worksheet ss:Name="Sheet1"> <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15"> <Row ss:AutoFitHeight="0"> <Cell><Data ss:Type="String">First Row</Data></Cell> </Row> <Row ss:AutoFitHeight="0"> <Cell><Data ss:Type="String">Second Row</Data></Cell> </Row> </Table> </Worksheet> </Workbook> 

由于有多个名称空间,所以您必须为XPATH定义这些名称空间。 即使是默认的命名空间,也必须这样做。 如果不是,则不能从使用XPATH的名称空间获取具体的元素。 这就是为什么/ //*将工作,但/行将无法正常工作,因为XPATH不知道哪一个命名空间Row属于。

设置名称空间将使用setProperty方法完成。 另请参阅二级DOM属性和SelectionNamespaces属性 。

你的例子:

 Set oXML = CreateObject("Microsoft.XMLDOM") oXML.aSync = false oXML.SetProperty "SelectionLanguage", "XPath" oXML.SetProperty "ServerHTTPRequest", True oXML.validateOnParse = False oXML.resolveExternals = False oXML.setProperty "SelectionNamespaces", "xmlns:d=""urn:schemas-microsoft-com:office:spreadsheet""" & _ " xmlns:o=""urn:schemas-microsoft-com:office:office""" & _ " xmlns:x=""urn:schemas-microsoft-com:office:excel""" & _ " xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""" & _ " xmlns:html=""http://www.w3.org/TR/REC-html40""" oXML.Load "_test_.xml" MsgBox oXML.SelectNodes("//d:Row").length ' Return 2 ' Looping through all rows in Table Set nodes = oXML.selectNodes("//d:Table//*") For i = 0 to nodes.length -1 Msgbox nodes(i).nodeName Next 

在这个例子中,我用d给默认的名称空间添加前缀。