String to HTMLdocument

String to HtmlDocument

The HtmlDocument class is a wrapper around the native IHtmlDocument2 COM interface.

You cannot easily create it from a string.

You should use the HTML Agility Pack.

Getting HtmlDocument from string without using browser control

You can use HtmlAgilityPack .... For example:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var results = doc.DocumentNode
.Descendants("div")
.Select(n => n.InnerText);

Converting a string to an HTML document

The reason is because you declared ahtm as Nothing. instantiate it and see if it works.

Update:
HtmlDocument is a wrapper around an unmanaged class (IHtmlDocument). Try Declaring a WebBrower and then assigning the ahtm to the web browser document property.

WebBrowser wb = new WebBrowser();
HtmlDocument atm = wb.Document;

In other words, Web browser is the easiest way.

Update:
The alternative would be to use something like HtmlAgilityPack. http://htmlagilitypack.codeplex.com/

How to convert raw html (string) to htmlDocument in Java

Checking source code of HTMLDocument gives you the answer.

It have a cool constructor to take html string.

public HTMLDocument(final String data) {
Charset cs = Charset.forName("utf-8");
this.data = data.getBytes(cs);
this.charset = cs;
}

So

HTMLDocument doc = new HTMLDocument(htmlStr);

C# Convert HtmlString into HTMLDocument

I think this StackOverflow discussion will help you.
You can find two ways to solve your problem:

  • with HTML Agility Pack
  • with the following code :

  HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;

doc2.write(fileText);

Creating an HTMLDocument from a String of HTML (in Java)

Try to use HtmlEditorKit class. It supports parsing of HTML content that can be read directly from String (e.g. through StringReader). There seems to be an article about how to do this.

Edit: To give an example, basically I think it could be done like this (aftrer the code is executed, htmlDoc should contain the loaded document...):

Reader stringReader = new StringReader(string);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
parser.parse(stringReader, htmlDoc.getReader(0), true);

Convert Html String into HTMLDocument VBA

I am making use of method from excel-vba-http-request-download-data-from-yahoo-finance:

Sub Forex(currency1 As String, currency2 As String)

Dim url As String, html As String, id As String
Dim oResult As Variant, oLine As Variant, sRate As String

url = "http://finance.yahoo.com/q?s=" & currency1 & currency2 & "=X"
id = "<span id=""yfs_l10_" & currency1 & currency2 & "=x"">"
html = GetHTTPResult(url)

oResult = Split(html, vbLf)
For Each oLine In oResult
If InStr(1, oLine, id, vbTextCompare) Then
sRate = Split(Split(oLine, "<span id=""yfs_l10_audusd=x"">")(1), "</span>")(0)
Exit For
End If
Next
End Sub

Function GetHTTPResult(sURL As String) As String
Dim XMLHTTP As Variant, sResult As String

Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
XMLHTTP.Open "GET", sURL, False
XMLHTTP.send
sResult = XMLHTTP.responseText
Set XMLHTTP = Nothing
GetHTTPResult = sResult
End Function

get all html as a String from HTMLDocument

StringWriter writer = new StringWriter();
kit.write(writer, doc, 0, doc.getLength());
String s = writer.toString();


Related Topics



Leave a reply



Submit