Parsing XML files in ARM Embedded Linux

Source: Internet
Author: User
Tags parse error xml parser
Recently, I am working on a project in arm-linux. I need to record some local data, logs, and other things. I was prepared to use a text file to record all the work and suddenly saw RSS, So I remembered XML. I have never used XML, so I plan to use XML to store this write data.
I have not read XML programming before, so I have read some documents. the current XML Parser generally has two types of parsing. one is the DOM model and the other is the sax2 model. the Dom model parses the structure in the XML file into a tree and then performs various operations. The sax2 model parses the XML file from scratch in a way similar to event processing. the two methods have their own advantages and disadvantages. However, the parser using the DOM model on an embedded device seems to consume too much memory, so it generally uses the sax2 parser. I use expat in Linux, which conforms to the sax2 model.
It seems that Linux has a library with expat, but I still wrote a source code package to port it to the arm board. you can use the cross-compilation method before learning the specific usage. A help file in the source code package has introduced in detail the usage and entry programming of each function in the library. basically, this is enough.
I checked the help and found that only 3 or 4 functions are needed to complete most of the work.
XML_Parser XML_ParserCreate(const XML_Char *encoding);

Create an XML object.

Xml_setelementhandler (xml_parser P, xml_startelementhandler start, xml_endelementhandler end );

Is a function for processing element headers and tails.

Xml_setcharacterdatahandler (xml_parser P, xml_characterdatahandler charhndl)
Sets the processing function of the text in the element.

Void xml_setuserdata (xml_parser P, void * userdata );
Set a shared data structure for each processing function.

Xml_status xml_parse (xml_parser P, const char * s, int Len, int isfinal );

This is the main processing function. specify a char array s containing XML text for the xml_parse object to parse XML. the parsing process is to call the processing function at a certain position for processing. for example, if you read the element header, you can call the start function set in xml_setelementhandler to process it.

Some of the holes described above are completely clear after reading an example:

To parse such an XML:

<Ifq name = "Wang jingtian" age = "22"> A greate guy! </Ifq>

The ATTR and content are displayed. The program is as follows:

Void starthandle (void * userdata, const char * El, const char ** ATTR)
{
// Parse the XML Element Header

Int I;
    for (i=0;attr[i];i+=2)
        {
            printf("%s=%s",attr[i],attr[i+1]);         
        }
 

}
void endhandle(void *data, const char *el)
 
{
// Do nothing.
}

Void chardatahandle (void * data, char * s, int Len)
{
// Display content. Note that S is not an identifier ending with an empty string. You must use Len to control it manually.
}

Int readxml (char * xmlbuf, int bufsize)
{

Xml_parser g_parser = xml_parsercreate (null );
Xml_setelementhandler (g_parser, starthandle, endhandle );
Xml_setcharacterdatahandler (g_parse, chardatahandle );
If (! Xml_parse (g_parser, xmlbuf, bufsize, 1 ))
{
Fprintf (stderr, "Parse error at line % d:/n % s/n ",
Xml_getcurrentlinenumber (g_parser ),
Xml_errorstring (xml_geterrorcode (g_parser )));
Return-1;
}
Xml_parserfree (g_parser );
Return 0;
} // The program is probably like this, but it is not debugged and cannot be guaranteed to be correct :)

The above is the entire processing program. It is to call readxml, and the XML Buf can be parsed. basically, the framework of all expat programs looks like this, and you can use it in your own program with slight changes.


The above is my summary of XML parsing, the ability is limited, if there is something wrong, please give more advice. Welcome to me and exchange. My Email: ifqqfi@gmail.com

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.