- 论坛徽章:
- 0
|
test.xml
- <?xml version="1.0"?>
- <story>
- <storyinfo>
- <author>John Fleck</author>
- <datewritten>June 2, 2002</datewritten>
- <keyword>example keyword</keyword>
- </storyinfo>
- <body>
- <headline>This is the headline</headline>
- <para>This is the body text.</para>
- </body>
- </story>
复制代码
获取所有节点名称的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
xmlNodePtr currentNode;
xmlNodePtr parseDoc(char *docname) {
xmlDocPtr doc;
xmlNodePtr cur;
xmlKeepBlanksDefault(0);
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
return cur;
}
void printChildrenNames(xmlNodePtr cur) {
if (cur != NULL) {
cur = cur->xmlChildrenNode;
while (cur != NULL){
printf("Current Node: %s\n", cur->name);
printChildrenNames(cur);
cur = cur->next;
}
return;
}else{
fprintf(stderr, "ERROR: Null Node!");
return;
}
}
int main(int argc, char **argv) {
char *docname;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
currentNode = parseDoc (docname);
//printf("Root Node: %s\n", currentNode->name);
printChildrenNames(currentNode);
return (1);
}
|
可是奇怪的是程序运行的结果是:
Current Node: storyinfo
Current Node: author
Current Node: text
Current Node: datewritten
Current Node: text
Current Node: keyword
Current Node: text
Current Node: body
Current Node: headline
Current Node: text
Current Node: para
Current Node: text
为啥会有这么多的text是哪来的?
[ 本帖最后由 brumby007 于 2008-12-8 11:19 编辑 ] |
|