Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created February 2, 2010 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/292934 to your computer and use it in GitHub Desktop.
Save tenderlove/292934 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
void dumpList(xmlDocPtr doc, xmlNodePtr list)
{
while(list) {
xmlElemDump(stdout, doc, list);
printf("\n#####\n");
list = list->next;
}
}
void xmlExample(const char * fragment)
{
printf("**** XML document behavior\n");
xmlDocPtr doc = xmlNewDoc("1.0");
xmlNodePtr ref = xmlNewNode(NULL, "root");
xmlDocSetRootElement(doc, ref);
xmlNodePtr list;
xmlParserErrors x = xmlParseInNodeContext(
ref,
fragment,
strlen(fragment),
XML_PARSE_RECOVER,
&list);
assert(x == XML_ERR_OK);
dumpList(doc, list);
}
void htmlExample(const char *fragment)
{
printf("**** HTML document behavior\n");
const char * htmldoc = "<html><body></body></html>";
htmlDocPtr doc = htmlReadMemory(htmldoc, strlen(htmldoc), NULL, NULL,
HTML_PARSE_RECOVER);
xmlNodePtr body = xmlDocGetRootElement(doc)->children;
assert(strcmp("body", body->name) == 0);
xmlNodePtr list;
xmlParserErrors x = xmlParseInNodeContext(
body,
fragment,
strlen(fragment),
XML_PARSE_RECOVER,
&list);
assert(x == XML_ERR_OK);
dumpList((xmlDocPtr)doc, list);
}
int main(int argc, char *argv[])
{
const char * fragment = "sir<div>phil</div>collins";
xmlExample(fragment);
htmlExample(fragment);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment