簡體   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