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



我正在尝试使用VBScript解析从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从命名空间中获取具体元素。这就是为什么//*可以工作,但//Row不能工作,因为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

最新更新