简体   繁体   English

在 C 中以 XML 打印节点

[英]Printing nodes in XML in C

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 .如果您不想要文本节点,则只打印XML_ELEMENT_NODE类型的节点。 That is, the print code should first check the type field:即打印代码首先要检查type字段:

if (node->type == XML_ELEMENT_NODE) {
    fprintf(stdout, "\t Child is <%s>\n", node->name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM