Created
August 28, 2009 18:46
-
-
Save tenderlove/177141 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 <libxml/HTMLParser.h> | |
#include <libxml/xmlsave.h> | |
/*** | |
* Write callback | |
*/ | |
int write_cb(void *ctx, char * buf, int len) { | |
printf("%s\n", buf); | |
return len; | |
} | |
int close_cb(void *ctx) { return 0; } | |
/*** | |
* When attempting to dump the meta tag, the output seems to be incorrect. | |
* This example finds the meta tag from the HTML document, then tries to | |
* print it to STDOUT. | |
* | |
* Using libxml2 2.7.3, my output is this: | |
* | |
* < ="" content=""></> | |
* | |
*/ | |
int main(int argc, char *argv[]) { | |
char * html = "\ | |
<html> \ | |
<head> \ | |
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \ | |
</head> \ | |
<body></body></html>"; | |
htmlDocPtr doc = htmlReadMemory(html, strlen(html), NULL, NULL, 1); | |
xmlNodePtr html_root = xmlDocGetRootElement(doc); | |
xmlNodePtr meta = html_root->children->children; | |
xmlSaveCtxtPtr savectxt = xmlSaveToIO( | |
(xmlOutputWriteCallback)write_cb, | |
(xmlOutputCloseCallback)close_cb, | |
NULL, | |
NULL, | |
1 | |
); | |
xmlSaveTree(savectxt, meta); | |
xmlSaveClose(savectxt); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment