I tried printing the nodes in the sample file. But i Keep getting a (text) printed after every node. Please help..
See the images tagged for information: photo photo
#include <stdio.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
int main(int argc, char **argv){
xmlDoc *document;
xmlNode *root, *first_child, *node, *sibling, *grand_child, *node2, *node3;
char *filename;
filename = argv[1];
document = xmlReadFile("sample.xml", NULL, 0);
root = xmlDocGetRootElement(document);
fprintf(stdout, "Root is <%s>\n", root->name);
first_child = root->children;
for (node = first_child; node!=NULL; node = node->next){
fprintf(stdout, "\t Child is <%s>\n", node->name);
grand_child=node->children;
for (node2= grand_child; node2!=NULL; node2 = node2->next){
fprintf(stdout, "\tGrand_Child is <%s>\n",node2->name);
sibling=node2->children;
for (node3= sibling; node3!=NULL; node3 = node3->next){
fprintf(stdout, "\t Sibling is <%s>\n",node3->name);
}
}
}
return 0;}
If you don't want the text nodes then only print the nodes with type XML_ELEMENT_NODE
. That is, the print code should first check the type
field:
if (node->type == XML_ELEMENT_NODE) {
fprintf(stdout, "\t Child is <%s>\n", node->name);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.