Use libxml2 library to parse XML files in C language in Ubuntu

Source: Internet
Author: User

To parse XML files in C language, you must first install the libxml2 Development Kit and use the following command to install

Sudo apt-Get install libxml +++ 1.0-Dev

Sudo apt-Get install libxml +++ 1.0-Doc

After installation, the header file <libxml/parser. h> can be included to use the libxml2 library in C.

Int xmlkeepblanksdefault (INT Val) // set whether to ignore blank nodes, such as spaces, which must be called before analysis. The default value is 0. It is best to set it to 1. otherwise there will be a painful debugging experience...

Xmldocptr xmlreadfile (const char * filename) // analyzes an XML file and returns a Document Object Pointer.

// Raise the basic structure of XML operations and its pointer type

 

Xmldoc: Structure of the xmldocptr Document Object and Its pointer xmlnode xmlnodeptr Node object Structure and Its pointer xmlattr xmlattrptr node attribute Structure and Its pointer xmlns xmlnsptr node namespace Structure and Its pointer

// Root node related functions

Xmlnodeptr xmldocgetrootelement (xmldocptr DOC) // get the document root node xmlnodeptr xmldocsetrootelement (xmldocptr doc, xmlnodeptr root) // set the document root node

// Create subnode Functions

Xmlnodeptr xmlnewnode (xmlnsptr NS, const xmlchar * Name) // create a new node xmlnodeptr xmlnewchild (xmlnodeptr parent, xmlnsptr NS, const xmlchar * Name, const xmlchar * content) // create a new sub-node xmlnodeptrxmlcopynode
(Const xmlnodeptr node, int extended) // copy the current node

 

 

// Add subnode related functions

Xmlnodeptr xmladdchild (xmlnodeptr parent, xmlnodeptrcur) // Add a subnode to a specified node xmlnodeptr xmladdnextsibling (xmlnodeptr cur, xmlnodeptr ELEM) // Add the next sibling node xmlnodeptr partition (xmlnodeptr)
Cur, xmlnodeptr ELEM) // Add the previous sibling node xmlnodeptrxmladdsibling (xmlnodeptr cur, xmlnodeptr ELEM) // Add the sibling Node

 

// Attribute-related functions

Xmlattrptr xmlnewprop (xmlnodeptr node, const xmlchar * Name, const xmlchar * value) // create a new node attribute xmlchar * xmlgetprop (xmlnodeptr node, const xmlchar * Name) // read the node attribute xmlattrptrxmlsetprop
(Xmlnodeptr node, const xmlchar * Name, const xmlchar * value) // set node attributes

After parsing the document, you need to call xmlfreedoc (xmldocptr) to release the resource,

When the content and attributes read by xmlnodegetcontent () and xmlgetprop () are used up, you need to call xmlfree () to release the used resources.

Libxml customizes a character type xmlchar, which is essentially an unsigned char.

In addition, libxml provides a macro to convert char * To xmlchar *. The name is very interesting. It is called bad_cast. Its essence is actually unsigned char *.

Libxml provides its own functions to facilitate the operation of strings of the xmlchar type. Their definition is similar to the string functions in the Standard C function library.

Xmlchar * xmlstrcat (xmlchar * cur, const xmlchar * Add)

Const xmlchar * xmlstrchr (const xmlchar * STR, xmlchar Val)

Int xmlstrcmp (const xmlchar * str1, const xmlchar * str2)

Int xmlstrlen (const xmlchar * Str)

Xmlchar * xmlstrncat (xmlchar * cur, const xmlchar * Add, intlen)

Int xmlstrncmp (const xmlchar * str1, const xmlchar * str2, intlen)
Const xmlchar * xmlstrstr (const xmlchar * STR, const xmlchar * val)

I believe that these functions are familiar to everyone. I will not explain them here.

For more functions, refer

Http://xmlsoft.org/html/libxml-xmlstring.html

The following is an example of reading all attributes and content,

Compile the command (directory of the file that needs to be added to the warehouse with the header): GCC test. C-o Test-lxml2-I/usr/include/libxml2

#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <libxml/parser.h>#include <libxml/xmlmemory.h>/////////////////////////////////////////////////int main(int argc, char **argv) {    xmlDocPtr doc;    xmlNodePtr curNode;    xmlChar *szKey, *szAttr;    xmlAttrPtr attrPtr;    int count = 0;    char szFile[512], szBuf[1024], key[256];            sprintf(szFile, "./test.xml");    doc = xmlReadFile(szFile, "UTF-8", XML_PARSE_RECOVER);    if (NULL == doc) {        fprintf(stderr, "open file failed!\n");        return -1;    }    curNode = xmlDocGetRootElement(doc);    if (NULL == curNode) {        fprintf(stderr, "Document not parsed sucessfully!\n");        xmlFreeDoc(doc);        return -1;    }    printf("root name: %s\n", curNode->name);    curNode = curNode->xmlChildrenNode;    while (curNode != NULL) {        count++;        szKey = xmlNodeGetContent(curNode);     // get the content        printf("%s\t", curNode->name);        attrPtr = curNode->properties;        while (attrPtr != NULL) {            szAttr = xmlGetProp(curNode, BAD_CAST attrPtr->name);            printf("%s:%s\t", attrPtr->name, szAttr);            strcpy(key, szAttr);            xmlFree(szAttr);            attrPtr = attrPtr->next;        }        printf("%s\t", szKey);        xmlFree(szKey);                curNode = curNode->next;    }    xmlFreeDoc(doc);        return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.