Created
April 13, 2010 04:02
-
-
Save flavorjones/364297 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
<html> | |
<body> | |
<div>hello <span>x</span> world.</div> | |
</body> | |
</html> |
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_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 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
/* | |
* 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