Created
June 1, 2013 11:21
-
-
Save khajavi/5690057 to your computer and use it in GitHub Desktop.
Example of getting attribute value of xml node by using libxml2 library.
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` | |
retrieve_attribute_value: retrieve_attribute_value_example.c | |
$(CC) retrieve_attribute_value_example.c -o retrieve_attribute_value_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
/* | |
* adopted from http://xmlsoft.org/tutorial/apg.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; | |
xmlDocPtr doc; | |
xmlNodePtr cur; | |
xmlChar *uri; | |
if (argc <= 1) { | |
printf("Usage: %s docname\n", argv[0]); | |
return(0); | |
} | |
docname = argv[1]; | |
doc = xmlParseFile(docname); | |
cur = xmlDocGetRootElement(doc); | |
cur = cur->xmlChildrenNode; | |
while (cur != NULL) { | |
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) { | |
uri = xmlGetProp(cur, "uri"); | |
printf("uri: %s\n", uri); | |
xmlFree(uri); | |
} | |
cur = cur->next; | |
} | |
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
<?xml version="1.0" encoding="utf-8"?> | |
<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> | |
<reference uri="www.w3.org/TR/xslt20/"/></story> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment