繁体   English   中英

在 C 中以 XML 打印节点

[英]Printing nodes in XML in C

我尝试打印示例文件中的节点。 但是我一直在每个节点之后打印一个(文本)。 请帮忙..

查看标记为信息的图像:照片照片

#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;}

如果您不想要文本节点,则只打印XML_ELEMENT_NODE类型的节点。 即打印代码首先要检查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