How can I read an XML file into a buffer in C?

后端 未结 8 820
逝去的感伤
逝去的感伤 2021-02-01 11:03

I want to read an XML file into a char *buffer using C.

What is the best way to do this?

How should I get started?

8条回答
  •  迷失自我
    2021-02-01 11:41

    1. Install libxml2 as a NuGet package in Visual studio(I am using Vs 2015 to test this)
    2. Copy and paste the contents under example XML file in a notepad and save the file as example.xml
    3. Copy and past the code under //xml parsing in to Vs
    4. Call the function from main with xml file name as an argument
    5. You will be getting the xml data in configReceive

    That's all...

    example XML file:

    
        
            
            
            
            
            
            
        
    

    Here is the source code:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    char configReceive[6][80] = { " " };
    
    //xml parsing
    void ParsingXMLFile(char *filename) {
        char         *docname;
        xmlDocPtr    doc;
        xmlNodePtr   cur;
        xmlChar      *uri;
        char config[6][80] = { "value1", "value2", "value3", "value4", "value5", "value6" };
        int count = 0;
        int count1 = 0;
    
        docname = filename;
        doc = xmlParseFile(docname);
        cur = xmlDocGetRootElement(doc);
        cur = cur->xmlChildrenNode;
        while (cur != NULL) {
            if ((!xmlStrcmp(cur->name, (const xmlChar *)"xmlConfig"))) {
                uri = xmlGetProp(cur, (xmlChar *)config[count++]);
                strcpy(configReceive[count1++], (char *)uri);
                xmlFree(uri);
            }
            cur = cur->next;
        }
    
        count = 0;
        count1 = 0;
        xmlFreeDoc(doc);
    }
    

提交回复
热议问题