问题
I'm working on a project for an embedded system that's using XML for getting data into and out of the system. I don't want the XML handling to devolve into a bunch of bits that build XML strings using snprintf()
/strcat()
and friends or parse XML by counting "<
" and ">
" characters.
I've found several XML libraries, a couple of which might even be small enough, but the closest they come to C is C++, which is not in the cards for this system. I hoping I can find an XML library that meets the following constraints:
- C source code
- no dynamic memory allocation
- cheap. Free is better, but copyleft won't do the trick.
It doesn't have to be a full parser - I just want to be able to pull text out of nested elements and have a reasonably simple way to generate XML that doesn't rely on format strings. Attributes aren't being used (yet), so the library doesn't need to support them even. The XML documents will be pretty small, so something that's DOM-like would be fine, as long as it'll work with client-provided buffers (parsing the raw XML in-place would be nice).
PugXML and TinyXML look to be pretty close, but I'm hoping that someone out there knows about an XML lib tailored just for C-based embedded systems that my googling is missing.
回答1:
I don't know about dynamic memory allocation, but a standard C XML parser is expat, which is the underlying library for a number of parsers out there.
回答2:
I am not sure but perhaps Mini-XML: Lightweight XML Library will help you:
- Mini-XML only requires an ANSI C compatible compiler.
- It is freeware.
- Its binary size is around 100k.
- It parses the whole xml-file and then store all the info into a linked list.
回答3:
You could use an ASN.1 XER encoder; there's a free one at http://lionet.info/asn1c/
回答4:
You could use the one from Gnome.
回答5:
I have written sxmlc to be as simple as possible and I know people use it in routers, to perform in-place parsing of web queries.
Unfortunately (and I'm 5 years late...) it does use memory allocation, though kept at a minimum: one buffer to read each "XML line" (what lies between <
and >
, sorry ;)), and many small buffer to keep track of the tag name, attributes names and values, and text (though I always wanted to use char[16]
or so for those).
And it makes use of strdup
/strcpy
and such.
As I want that anybody can use it freely, the licence is BSD.
回答6:
Xerces-C library would be optimal to use, in this scenario.
If it is going to be pretty small XML, why not generate programatically, using sprintf or other stuff and use string extracting functions to parse the same. But as mentioned earlier, if little big, would suggest to use Xerces-c Library, as it is open source
来源:https://stackoverflow.com/questions/1131701/c-xml-library-for-embedded-systems