开发者

A simple C XML parser [closed]

Closed开发者_JAVA技巧. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 months ago.

Improve this question

I need to read an XML formatted document from a C program and extract from it the elements and their values. For example in the following code:

<user name="Mark">
    <param name="Age" value="21"/>
    <param name="Country" value="NL"/>
</user>

I need to extract: name = Mark, Age = 21 and Country = NL.

Up until today I've been doing this parsing manually which is a pain.

I don't care whether the file a "proper XML" or all that, I don't care about DTD's or other standard XML requirements. I just need to read and parse the values.

Does anyone know of a library other than lib eXpat to do this or code to do this?

Thanks!


Libxml2


The Expat parser is the best I've come across - I use it in my C++ code in preference to the various C++ parsers - but it is written in C. Very easy to use and embed in your application. So I don't see why in your question you say:

(other than lib eXpat)

do you have something against it?


How about Mini-XML? It's lightweight, works with gcc, is ANSI-C compatible...

http://www.minixml.org/index.php

According to the documentation, to search for specific nodes would be as simple as:

/* Find the first "a" element */
    node = mxmlFindElement(tree, tree, "a",
                           NULL, NULL,
                           MXML_DESCEND);

Once you get the node, you can manipulate it according to your requirements.


If C++ is OK then you might try TinyXML. I've been using it for a few years and it works nicely.


Mini-XML looks promising

http://www.minixml.org/


You can consider miniML-Parser, a simple and tiny XML parser library in C.

  • Unlike other XML parser it is extremely easy to use. You need to call only one API to parse your XML data.
  • It is a validating parser. It validates the syntax of XML data and also extracts the content of XML data.

In order to parse and extract the content from XML data you need to provide some information to the parser. for example XML element name, its child elements, attributes, content type etc. Parser uses xs_element_t structure to hold all these properties of XML data

In your example, you have 2 XML elements "user" and "param". user is a root element and param is a child element. Attribute "name" and "value" holds the content. In order to extract these content, you need to specify the content type and target address to store the content.

Example of xs_element_t for your XML data.

const xs_element_t user_root =
{
    .Name.String = "user",                 //!< name of XML element
    .Name.Length = 4,                      //!< Length of name
    
    .Attribute_Quantity = 1,               //!< Number of attributes of this element
    .Attribute          = attributes,      //!< Address of structure containing attributes

    .Child_Quantity = 1,                   //!< Number of child elements
    .Child          = &param_element,      //!< Address of structure holding child elements
};

const xs_element_t param_element =
{
    .Name.String = "param",                //!< name of XML element
    .Name.Length = 5,                      //!< Length of name

    .Attribute_Quantity = 2,               //!< Number of attributes of this element
    .Attribute          = attributes,      //!< Address of structure containing attributes    
};

//! Holds properties of attributes
const xs_attribute_t attributes[] =
{
    [0].Name.String = "name",           //!< Name of XML attribute
    [0].Name.Length = 4,                //!< Length of attribute name

    [0].Target.Type = EN_STATIC,        //!< Target address type is static.
    [0].Target.Address = &name,         //!< Target address offset from the parent target address
    [0].Content.Type   = EN_STRING,     //!< Content type is string.             

    [1].Name.String = "value",          //!< Name of XML attribute
    [1].Name.Length = 4,                //!< Length of attribute name

    [1].Target.Type = EN_STATIC,        //!< Target address type is static.
    [1].Target.Address = &value,        //!< Target address offset from the parent target address
    [1].Content.Type   = EN_STRING,     //!< Content type is string.             
};

Note: Above structure is not complete. I have omitted some of the structure members to keep the example simple.

To parse xml data then call parse_xml() and pass address of user_root (root element) and XML data.

parse_xml(&user_root, xml_data, NULL);

You can also registers a callback function in the xs_element_t structure to get callback whenever parser encounters element/tag during parsing.

for further details refer https://github.com/kiishor/miniML-Parser

Disclosure: I'm author of this miniML-Parser

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜