Created
February 2, 2010 19:25
-
-
Save tenderlove/292934 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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