sunmoon
sunmoon

Reputation: 31

C: How to extract the doctype of a HTML page

I need to extract the doctype of a HTML page which may be XHTML, HTML html or WML, using C or C++. I will be giving the input as a HTML file or as an array.

if html pages does't contain header then result should be in with respect to page like if it is html result = html or if it is xhtml result = xhtml....

Upvotes: 0

Views: 220

Answers (1)

gcbenison
gcbenison

Reputation: 11963

This seems like two distinct questions:

1) how to simply grab the "doctype" declaration from an html page, for which I was going to suggest something simple like:

char doctype[1024];

void
get_doctype(char *html_page)
{
  sscanf(html_page, "<!DOCTYPE %1024s>", doctype);
}

Then perhaps match against known doctype strings to get an enumerated value.

But you're also asking 2) how to detect the type of a page with no doctype declaration. That's harder, and there may be multiple correct answers for each page. I would suggest outsourcing to a library like libxml. It has functions to validate input streams as certain types of documents.

Upvotes: 1

Related Questions