libxml2操作2-获取属性值

上一篇是获取了节点的值,这一次获取属性的值:
libxml2操作2-获取属性值
文章图片

文件如下:

John Fleck June 2, 2002 example keyword This is the headlineThis is the body text.

例子如下:
1 #include 2 #include 3 #include 4 #include 5 #include 6 7 void 8 getReference (xmlDocPtr doc, xmlNodePtr cur) { 9printf("enter function getReference\r\n"); 10xmlChar *uri; 11cur = cur->xmlChildrenNode; 12while (cur != NULL) { 13if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) { 14uri = xmlGetProp(cur, "uri"); 15printf("uri: %s\n", uri); 16xmlFree(uri); 17} 18cur = cur->next; 19} 20printf("exit function getReference\r\n"); 21return; 22 } 23 24 25 void 26 parseDoc(char *docname) { 27 28xmlDocPtr doc; 29xmlNodePtr cur; 30 31doc = xmlParseFile(docname); 32 33if (doc == NULL ) { 34fprintf(stderr,"Document not parsed successfully. \n"); 35return; 36} 37 38cur = xmlDocGetRootElement(doc); 39 40if (cur == NULL) { 41fprintf(stderr,"empty document\n");

编译如下:
root@mkx:~/workspace/libxml2/learn.20211112# gcc -o example_Retrieviing example_Retrieviing.c -L/usr/local/lib -lxml2 -L/usr/local/lib -lz -lm -ldl -I/usr/local/include/libxml2

【libxml2操作2-获取属性值】运行如下:
root@maokx:~/workspace/libxml2/learn.20211112# ./example_Retrieviing story.xml enter function getReference uri: storyuri_example1 exit function getReference

    推荐阅读