XML to C struct and C struct to XML

17,335

Solution 1

He is not asking for an XML parser. He is asking for a library that automatically serializes/unserializes a complex C structure to and from XML (which obviously would rely on an XML parser underneath).

Libpdel is old but has support for doing what you want (via the "structs" stuff).

Solution 2

You aren't going to be able to parse XML in the general case in only a few hundred lines of code. There are several XML parser libraries out there, of which expat comes to mind. Expat was written in C, and has a C-friendly API.

Serialization is likely to be easier, assuming you don't go nuts with the data types you support.

Either way, where this is going to cause you headaches is in the maintenance of the correspondence between struct layout and XML schema.

You might want to look into libraries like SCEW that aim to conceal the event-driven nature of expat's implementation and present something more like a DOM tree. There are also various libraries that implement SOAP on top of expat, and those necessarily need to handle marshaling of data in and out of XML packets.

Solution 3

One way you could do it if you don't find any premade libraries or code is to write a toXML() function like the usual toString() functions. Then a toStruct(char*) that deserializes the XML back into the struct

Share:
17,335
Matthias Wandel
Author by

Matthias Wandel

Updated on June 24, 2022

Comments

  • Matthias Wandel
    Matthias Wandel almost 2 years

    I like to do my server side programming in C, but need to inter-operate with some XML.

    What I need to write is some function that, given a C structure, or nested structure, and another structure (or nested structures) that describes the elements in the C structure, spits it out as XML. And another function that reads the XML, verifies that it matches the description of the C structures, and populates the C structures.

    I'm quite certain this has been done many times before, but there is so much other info about XML out there that I'm not having any luck composing a Google query that doesn't return a lot of unrelated stuff.

    I'm not looking for a library - just a few hundred lines of C code to parse the XML.

  • Matthias Wandel
    Matthias Wandel almost 15 years
    Yes, something like that. And that's what I'm hoping to avoid having to write if there is code out there that does that.
  • bortzmeyer
    bortzmeyer almost 15 years
    Well, parsing the XML is the easy part (and there are many libraries that are easier than expat such as libxml). THe hard part is the two-way mapping/conversion between C structs and XML.
  • Josh Petitt
    Josh Petitt about 11 years
    @RBerteig, do you have any resources for the struct layout and XML schema? I believe this is the crux of the question (and mine as well). Ideally I'm looking for the subset of XML that will always map to a C structure and vice versus. I realize the XML document and C structure are fundamentally different. However, I'm hoping to find common ground between the two that could be used as a spec.
  • RBerteig
    RBerteig about 11 years
    @JoshPetitt, I don't have any resources offhand. I would start by mapping a simple struct to a tag with attributes. That works well for things like struct point{ int x; int y; } which could be written <point x='5' y='17'>. That works for POD with no pointers and data types that can be written in (very) plain text. Another tag can be used to collect arrays. It gets more interesting when you need to represent arbitrary string data, structures other than a tree, pointers at all, and so forth.... don't forget about security issues, either. Safe Serialization is Hard.
  • Josh Petitt
    Josh Petitt about 11 years
    @RBerteig, thanks for the tips. I am good at C, but not as familiar with the details of XML (my knowledge of XML is mainly practical, working knowledge). I was hoping for an XML expert to provide a schema that I could code my C application to. I don't use XML on a server or anything that uses a DTD.
  • RBerteig
    RBerteig about 11 years
    @JoshPetitt This is might case where JSON may be a better answer unless you have an external requirement that is met only by XML. There are several C libraries for JSON, and the format is simple enough to be read by humans since it is a carefully chosen subset of javascript's representation for structured data. There are lots of SO questions related to JSON and its use.
  • Josh Petitt
    Josh Petitt about 11 years
    @RBerteig, yes I am also supporting JSON, which is MUCH easier because there is a fairly straightforward mapping and similarities in syntax. Also, JSON is "structured data" as opposed to a "document" (AFAIK) which also makes the concepts easier. Thanks for the suggestion.
  • CoffeeTableEspresso
    CoffeeTableEspresso about 5 years
    Good answer, although I disagree with your statement about it taking a lot of effort to parse XML.