read_xml
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
int parseStory(xmlDocPtr doc, xmlNodePtr cur)
{
xmlChar *key;
cur = cur->xmlChildrenNode;
while(cur != NULL)
{
//if((!xmlStrcmp(cur->name, (const xmlChar*)"abc")))
//{
// printf("[%s]\n", cur->name);
// parseStory(doc, cur);
//}
if((!xmlStrcmp(cur->name, (const xmlChar*)"mysql")))
{
printf("[%s]\n", cur->name);
parseStory(doc, cur);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"host")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"port")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"db")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"username")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"password")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"credits")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
else if((!xmlStrcmp(cur->name, (const xmlChar*)"instructor")))
{
key=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf(" %s=>%s\n", cur->name, key);
xmlFree(key);
}
cur = cur->next;
}
return 0;
}
int main(int argc, char **argv)
{
//定义2个指针,doc指向整个dom;cur指向结点,以后遍历树就靠这个指针了
xmlDocPtr doc;
xmlNodePtr cur;
//获取doc指针,也是把其他格式转成utf8的功能吧
doc=xmlParseFile(argv[1]);
if(doc==NULL)
{
printf("Document not parsed successfully. \n");
exit(1);
}
printf("xmlParseFile ok.\n");
// 取得结点指针
cur=xmlDocGetRootElement(doc);
if(cur==NULL)
{
printf("empty document. \n");
xmlFreeDoc(doc);
exit(1);
}
printf("xmlDocGetRootElement ok.\n");
// 取得根结点指针,我这里是root,记住,这里一定要是根结点
if (xmlStrcmp(cur->name, (const xmlChar *)"root"))
{
printf("document of the wrong type, root node != root\n");
xmlFreeDoc(doc);
exit(1);
}
printf("ok.\n");
//通过这个递归函数,遍历出所有感兴趣的结点。
parseStory(doc, cur);
//一定要释放doc哦。
xmlFreeDoc(doc);
exit(0);
}
补充:综合编程 , 其他综合 ,