c语言如何解析xml并将所有内容存入数组

如题所述

/* å‰æ®µæ—¶é—´æ°å¥½åšè¿‡ç±»ä¼¼çš„东西,代码可以给你参考下。
 *  Xml配置见最后
 */

typedef struct SrcFileFmt
{
    int   ColID;
    char  ColCode[64];      /* å­—段英文名称 */
    char  ColName[128];     /* å­—段中文名称*/
    char  ColType[20];      /* å­—段类型(包含长度) */
    char  ColComment[128];  /* å­—段描述 */
}SrcFileFmt;

int main(int argc, char **argv)
{
    SrcFileFmt SrcFileFmt[128];
    int iNum = -1;
    if ( 2 > argc )
    {
        printf("Usage: %s SrcXmlFile\n", argv[0]);
        return -1;
    }
    iNum = parseSourceCfg(SrcCfgFile, SrcFileFmt);
    if (iNum == -1)
    {
        return -1;
    }
    return 0;
}

/* è°ƒç”¨æ­¤å‡½æ•°åŽï¼Œxml文件的内容会被存储到结构体数组SrcFileFmt srcfilefmt[]中
 * æ­¤å‡½æ•°ä¾èµ–于libxml2-2.9.2.tar.xz
 */
int parseSourceCfg(char *FileName, SrcFileFmt srcfilefmt[])
{ /* è§£æžæºæ–‡ä»¶xml,FileName ä¸ºæºxml文件名 */
    xmlDocPtr doc;
    xmlNodePtr cur, root;
    char sFileName[64] = {'\0'};
    int cnt = 0;
    if (FileName == NULL)
    {
        return -1;
    }
    sprintf(sFileName, "%s.xml", FileName);
    doc = xmlParseFile(sFileName);
    if (doc == NULL)
    {
        return -1;
    }
    root = xmlDocGetRootElement(doc);
    if (root == NULL) {
        xmlFreeDoc(doc);
        return(-1);
    }
    if (xmlStrcmp(root->name, (const xmlChar *) "SrcRoot"))
    {
        xmlFreeDoc(doc);
        return -1;
    }
    
    cur = root->xmlChildrenNode;
    while (cur != NULL) 
    {
        if ((!xmlStrcmp(cur->name, (const xmlChar *)"Column")))
        {
            xmlChar *key;
            xmlNodePtr cur_sub = cur;
            cur_sub = cur_sub->xmlChildrenNode;

            while (cur_sub != NULL) 
            {
                if ((!xmlStrcmp(cur_sub->name, (const xmlChar *)"ColID"))) {
                    key = xmlNodeListGetString(doc, cur_sub->xmlChildrenNode, 1);
                    killblank((char*)key);
                    srcfilefmt[cnt].ColID = atoi((char*)key);
                    xmlFree(key);
                }
                if ((!xmlStrcmp(cur_sub->name, (const xmlChar *)"ColCode"))) {
                    key = xmlNodeListGetString(doc, cur_sub->xmlChildrenNode, 1);
                    killblank((char*)key);
                    strcpy(srcfilefmt[cnt].ColCode, (char*)key);
                    xmlFree(key);
                }
                else if ((!xmlStrcmp(cur_sub->name, (const xmlChar *)"ColName"))) {
                    key = xmlNodeListGetString(doc, cur_sub->xmlChildrenNode, 1);
                    killblank((char*)key);
                    strcpy(srcfilefmt[cnt].ColName, (char*)key);
                    xmlFree(key);
                }
                else if ((!xmlStrcmp(cur_sub->name, (const xmlChar *)"ColType"))) {
                    key = xmlNodeListGetString(doc, cur_sub->xmlChildrenNode, 1);
                     killblank((char*)key);
                    strcpy(srcfilefmt[cnt].ColType, (char*)key);
                    xmlFree(key);
                }
                else if ((!xmlStrcmp(cur_sub->name, (const xmlChar *)"ColComment"))) {
                    key = xmlNodeListGetString(doc, cur_sub->xmlChildrenNode, 1);
                    killblank((char*)key);
                    strcpy(srcfilefmt[cnt].ColComment, (char*)key);
                    xmlFree(key);
                }
                cur_sub = cur_sub->next;
            }
            cnt++;
        }
        cur = cur->next;
    }
    xmlFreeDoc(doc); 
    return cnt;
}

<SrcRoot>
    <Column>
        <ColID>1</ColID>
        <ColCode>kmh</ColCode>
        <ColName>字段1</ColName>
        <ColType>VARCHAR(11)</ColType>
    </Column>
    <Column>
        <ColID>2</ColID>
        <ColCode>dfkmh</ColCode>
        <ColName>字段2</ColName>
        <ColType>VARCHAR(11)</ColType>
    </Column>
    <Column>
        <ColID>3</ColID>
        <ColCode>hbh</ColCode>
        <ColName>字段3</ColName>
        <ColType>INTEGER(10)</ColType>
    </Column>
</SrcRoot>
温馨提示:内容为网友见解,仅供参考
第1个回答  2016-07-29
可以试试libxml追问

使用了libxml2 解析做好了 但是xml所有内容保存在哪了

追答

你可以看文档,也可以试一下这个网页:
http://www.cnblogs.com/shanzhizi/archive/2012/07/09/2583739.html
或者试试这个:
http://www.cnblogs.com/Anker/p/3542058.html

相似回答