XML parser implemented in C language

Source: Internet
Author: User
Tags log xml parser

Recently do embedded development, the board needs to have the function of parsing XML, I will go to the Internet to find open source to use. The result is either C + + or super complex. Like Libxml, I count the next parse a 40 KB of XML files, incredibly dynamic application memory 100 times, for no MMU function of the ARM7, it is not a blessing to digest.

So, I can only write one to use.

I wrote this XML parser, very simple, the core code is only more than 600 lines. Of course, the function is also relatively weak, only the ANSI-encoded XML file, can only parse, can not be generated.

The entire parser uses only a few EBNF grammars and a DFA state machine (used to skip annotations), using a recursive descent analysis method.

The entire parsing process does not dynamically request memory, does not cause memory fragmentation (especially for devices without MMU), and of course, before beginning parsing, it is necessary to provide a buffer for the parser, the last generated XML tree, placed in this buffer.

The interface is as follows:

/******************************************************
/* minixml.h
/*
/* author:@#$%^&*
/*
* About:
/* This file provides functionality based on parsing XML file
/*
/*
/* Sample:
/*
/* Support platform: Windows, Linux 2.4,2.6 uclinux
/* [in] means an input parameter
/* [out] represents an output parameter
/******************************************************/
#ifndef _minixml_h_08_17_
#define _minixml_h_08_17_
#define _crt_secure_no_deprecate
#if defined (__cplusplus) | | Defined (c_plusplus)
extern "C" {
#endif
struct _mini_xml_attri;
struct _mini_xml_node;
/* Log an attribute information/*
typedef struct _MINI_XML_ATTRI
{
char* name; /* Property Name * *
char* value; /* Attribute Value * *
struct _mini_xml_attri* next; /* point to next attribute * *
} Mini_xml_attri;
/* Log an XML node information * *
typedef struct _MINI_XML_NODE
{
char* name; /* Section name * *
char* value; /* Node Value * *
Mini_xml_attri* attri_list; /* Attribute list * *
struct _mini_xml_node* parent; /* Parent Node * *
struct _mini_xml_node* child; /* child node, if not empty * *
struct _mini_xml_node* next; /* The next node in the same layer * *
} Mini_xml_node;
  
////////////////////////////////////////////////////
Description: Parse XML file, return XML root node
Parameters:
: XML XML file path [in]
: Buffer for resolution of buffers [in]
: Buffer_len buffer size (in bytes) [in]
: Error_reason Save error reason when executing error [in]
: root node [out] of the root XML
Return: Successfully returned 0, failure returned-1
Description
: Q: What is the appropriate buffer for parsing?
A: The buffer for parsing is mainly used for storing XML trees, so Buffer_len >= (size of the XML file) * 2 can
/// :
int Mini_parse_xml (char* XML,
char* buffer, int buffer_len,
Char error_reason[128],
mini_xml_node** root);
////////////////////////////////////////////////////
Description: Find child nodes of a specific node
Parameters:
: Father parent node [in]
: Name Child child section Roll Call [in]
: Child children node [out]
int Mini_find_child (mini_xml_node* father, char* name, mini_xml_node** child);
////////////////////////////////////////////////////
Description: Finds property values for a specific node
Parameters:
: node node [in]
: Name property name [in]
: Value property values [out]
int Mini_find_attribute (mini_xml_node* NODE, char* name, char** value);
////////////////////////////////////////////////////
Description: Print XML tree for trial
int Mini_print_tree (mini_xml_node* root, int layer);
#if defined (__cplusplus) | | Defined (c_plusplus)
}
#endif
#endif

Related Article

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.