For example I want to connect to http://www.google.com and get DOM.

In Anonymous Window I have code:

        String url = 'http://www.google.com/';
        HttpRequest req = new HttpRequest(); 
        Http https = new Http(); 

        HttpResponse res = https.send(req);

        //String str = res.getBody();
        Dom.Document doc = res.getBodyDocument();

But I get the error from getBodyDocument()

System.XmlException: unexpected markup <!d (position: START_DOCUMENT seen <!d... @1:3)

before <!doctype >

getBody() returns < and > (not &lt; and &gt;).

Even though I tried replace from &lt; and &gt; to < and >.


Any site. Maybe any site)

1 Answer 1


Websites emit HTML and historically browsers have been very tolerant of improperly structured HTML. So while it would be helpful to programmers if the HTML was also structured to be well-formed XML so that it could be parsed as you are attempting, usually the HTML is not.

In your case the issue you are hitting is the doctype declaration at the beginning. (It is probably the error message that is converting the characters to their entity versions.) You could remove that e.g.:

String str = res.getBody().substring('<!doctype html>'.length());
Dom.Document doc = new Dom.Document();

but the next error I hit was an entity error and there may well be many other errors for the Google URL or any other URL.

You may have to just find the content you are looking for in the HTML using e.g. regular expressions (i.e. avoiding the XML parsing).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .