Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created August 28, 2009 18:46
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/177141 to your computer and use it in GitHub Desktop.
Save tenderlove/177141 to your computer and use it in GitHub Desktop.
#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