4

I have stored a webpage's HTML in the database.

I want to take advantage of HtmlUnit's ability to find/reference DOM elements.

Is it possible to load the HtmlPage object from a string (via a database column)?

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
mrblah
  • 99,669
  • 140
  • 310
  • 420
  • just for interest: why are you persisting complete html-pages as strings inside the database? why not just store as simple file to filesystem? – manuel aldana Jan 07 '10 at 01:30

3 Answers3

8

StringWebResponse may help.

Edit: example:

    URL url = new URL("http://www.example.com");
    StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url);
    HtmlPage page = HTMLParser.parseHtml(response, new TopLevelWindow("top", new WebClient()));
    System.out.println(page.getTitleText());
Mirko N.
  • 10,537
  • 6
  • 38
  • 37
4

I assume you're using HtmlParser.parseHtml to create the HtmlPage object and just need a WebResponse to pass to it?

If so, StringWebResponse will wrap your string so you can pass it directly to parseHtml.

ZoogieZork
  • 11,215
  • 5
  • 45
  • 42
  • I can't find HtmlParser in HtmlUnit, are you referring to another lib? – mrblah Jan 06 '10 at 22:41
  • http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HTMLParser.html#parseHtml%28com.gargoylesoftware.htmlunit.WebResponse,%20com.gargoylesoftware.htmlunit.WebWindow%29 – Mirko N. Jan 06 '10 at 22:44
  • 1
    Problem is that TopLevelWindow constructor is protected – lisak May 26 '11 at 09:23
0

uhhmm well yes.

You just need to serve it ( if you're using java you can use Tomcat for that ) and point your test to the served page.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569