Parsing XHTML with XPATH using Microsoft.XMLHTTP in VBScript

丶灬走出姿态 提交于 2019-12-23 05:59:26

问题


I'm looking to parse an xhtml document with Microsoft.XMLHTTP with XPATH in VBScript. I have the following xhtml document structure. How would I get an array of the urls?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Local index</title>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <a href="url1.html">url1</a><br/>
                    <a href="url2.html">url2</a><br/>
                    <a href="url3.html">url3</a>
                </td><td>
                    <a href="url1-1.html">url1-1</a><br/>
                    <a href="url2-1.html">url2-1</a><br/>
                    <a href="url3-1.html">url3-1</a>
                </td>
            </tr>
        </table>
    </body>
</html>

回答1:


Are you sure you need to use the antiquated program id Microsoft.XMLHTTP? These days both MSXML 3 as well as MSXML 6 are part of the OS respectively supported service packs with anything since Windows XP. As for using XPath and MSXML 3, here is an example:

Dim doc
Set doc = CreateObject("Msxml2.DOMDocument.3.0")
doc.validateOnParse = False
doc.resolveExternals = False
If doc.load("file.xml") Then
  doc.setProperty "SelectionLanguage", "XPath"
  doc.setProperty "SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml'"
  For Each link In doc.selectNodes("//xhtml:a")
    WScript.Echo(link.getAttribute("href") & ": " & link.text)
  Next
Else
  WScript.Echo(doc.parseError.reason)
End If


来源:https://stackoverflow.com/questions/9822520/parsing-xhtml-with-xpath-using-microsoft-xmlhttp-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!