Skip to content

Instantly share code, notes, and snippets.

@flavorjones
Created April 13, 2010 04:02
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 flavorjones/364297 to your computer and use it in GitHub Desktop.
Save flavorjones/364297 to your computer and use it in GitHub Desktop.
<html>
<body>
<div>hello <span>x</span> world.</div>
</body>
</html>
INCLUDE_PATH=/home/mike/.multixml2/install/libxml2-2.7.3/include/libxml2
LIBRARY_PATH=/home/mike/.multixml2/install/libxml2-2.7.3/lib
replace-bug: replace-bug.c
gcc replace-bug.c -o replace-bug -I$(INCLUDE_PATH) -Bstatic -L$(LIBRARY_PATH) -lxml2
/*
* this program illustrates a possible libxml2 API bug, wherein xmlReplaceNode
* does not merge adjacent text nodes.
*/
#include <stdio.h>
#include <libxml/xpath.h>
#include <libxml/xmlsave.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
int main(int argc, char**argv)
{
htmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodePtr parent,
to_be_replaced,
replacer ;
xmlNodePtr child ;
printf("parsing 'foo.html'\n");
doc = htmlReadFile("foo.html", NULL, 0);
xpathCtx = xmlXPathNewContext(doc);
xpathObj = xmlXPathEvalExpression("/html/body/div", xpathCtx);
parent = xpathObj->nodesetval->nodeTab[0];
printf("before replace, children of node are: ");
child = parent->children ;
while (child) {
printf("%s ", child->name);
child = child->next ;
}
printf("\n");
xpathCtx = xmlXPathNewContext(doc);
xpathObj = xmlXPathEvalExpression("/html/body/div/span", xpathCtx);
to_be_replaced = xpathObj->nodesetval->nodeTab[0];
replacer = xmlNewText("there, ");
xmlReplaceNode(to_be_replaced, replacer);
printf("after replace, children of node are: ");
child = parent->children ;
while (child) {
printf("%s ", child->name);
child = child->next ;
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment