tenderlove (owner)

Revisions

gist: 209686 Download_button fork
public
Public Clone URL: git://gist.github.com/209686.git
Embed All Files: show embed
test.c #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/HTMLparser.h>
 
void find_with_id(char * html)
{
  printf("parsing: %s\n\n", html);
  htmlDocPtr doc = htmlReadMemory(html, strlen(html), NULL, NULL, 1);
 
  xmlXPathContextPtr ctx = xmlXPathNewContext(doc);
  xmlXPathObjectPtr xpath = xmlXPathEvalExpression("id('hello')", ctx);
 
  if(doc->intSubset)
    printf("intSubset: %s\n", doc->intSubset->name);
  if(doc->extSubset)
    printf("extSubset: %s\n", doc->extSubset->name);
 
  printf("found %d\n", xpath->nodesetval->nodeNr);
}
 
int main(int argc, char *argv[])
{
  char * doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html><body><p id='hello'>blah</p></html>";
 
  char * no_doctype = "<html><body><p id='hello'>blah</p></html>";
 
  // Finds the p tag with the ID
  find_with_id(doctype);
 
  // Does not find the p tag with the ID
  find_with_id(no_doctype);
 
  return 0;
}