本文介绍了MSHTML:CreateDocumentFromString 而不是 CreateDocumentFromUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 MSHTML 库来解析字符串变量中的一些 HTML.但是,我无法弄清楚如何做到这一点.我可以轻松地解析给定 URL 的网页内容,但不能直接解析源 HTML.这可能吗?如果有,怎么做?

I'd like to use the MSHTML library to parse some HTML that I have in a string variable. However, I can't figure out how to do this. I can easily parse the contents of a webpage given a known URL, but not the source HTML directly. Is this possible? If so, how?

Public Sub ParseHTML(sHTML As String)
Dim oHTML As New HTMLDocument, oDoc As HTMLDocument

    'This works:'
    Set oDoc = oHTML.createDocumentFromUrl("http://www.google.com", "")

    'I would like to do the following but no such method actually exists:'
    Set oDoc = oHTML.createDocumentFromString(sHTML)

    ....
    'Parse the HTML using the oDoc variable'
    ....

推荐答案

可以;

Dim odoc As Object

Set odoc = CreateObject("htmlfile") '// late binding

'// or:
'// Set odoc = New HTMLDocument
'// for early binding

odoc.open
odoc.write "<p> In his house at R'lyeh, dead <b>Cthulhu</b> waits dreaming</p>"
odoc.Close
MsgBox odoc.body.outerHTML

这篇关于MSHTML:CreateDocumentFromString 而不是 CreateDocumentFromUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 20:11