Created
June 1, 2013 10:57
-
-
Save khajavi/5690010 to your computer and use it in GitHub Desktop.
Example of adding new keyword tag to specific xml node by libxml2
This file contains hidden or 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
/* | |
* adopted from http://xmlsoft.org/tutorial/ape.html | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <libxml/xmlmemory.h> | |
#include <libxml/parser.h> | |
int | |
main(int argc, char **argv) { | |
char *docname; | |
char *keyword; | |
xmlDocPtr doc; | |
xmlNodePtr cur; | |
if (argc <= 2) { | |
printf("Usage: %s docname, keyword\n", argv[0]); | |
return(0); | |
} | |
docname = argv[1]; | |
keyword = argv[2]; | |
doc = xmlParseFile(docname); | |
cur = xmlDocGetRootElement(doc); | |
cur = cur->xmlChildrenNode; | |
while (cur != NULL) { | |
if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ | |
xmlNewTextChild (cur, NULL, "keyword", keyword); | |
} | |
cur = cur->next; | |
} | |
if (doc != NULL) { | |
xmlSaveFormatFile (docname, doc, 1); | |
xmlFreeDoc(doc); | |
} | |
return (1); | |
} | |
This file contains hidden or 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
CC = gcc | |
CLIBS = `pkg-config libxml-2.0 --cflags --libs` | |
add_keyword: add_keyword_example.c | |
$(CC) add_keyword_example.c -o add_keyword_example.bin $(CLIBS) | |
clean: | |
rm -f *.o *.bin |
This file contains hidden or 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
<?xml version="1.0"?> | |
<story> | |
<storyinfo> | |
<author>John Fleck</author> | |
<datewritten>June 2, 2002</datewritten> | |
<keyword>تخیلی</keyword> | |
</storyinfo> | |
<body> | |
<headline>This is the headline</headline> | |
<para>This is the body text.</para> | |
</body> | |
</story> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment