tags:

views:

126

answers:

5

This is what I need to do: I need to read an XML formatted document 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.

Now 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.

Anyone knows (other than lib eXpat) a lib to do this or code to do this? Thanks!

Jess

EDIT:

Yes, I forgot to mention the platform: Windows and Linux. In plain C, not C++

+5  A: 

Libxml2

WhirlWind
thanks, I already checked this and 1) it took me forever to understand what they wanted from me (in order to make it work) and 2) it complains that I don't have a DTD and that my file is not properly formatted. so, not a good solution.
Jessica
I was thinking something more along the lines of http://www.codeguru.com/cpp/data/data-misc/xml/article.php/c4549
Jessica
Steven D. Majewski
A: 

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

Len Holgate
thanks but it's plain C
Jessica
Fair enough....
Len Holgate
A: 

Mini-XML looks promising

http://www.minixml.org/

Jessica
+1  A: 

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?

anon
I indeed, I would definitely choose expat considering the given requirements. The only reason not to use it in this case that I can think of is if the poster has no grasp of creating data structures from SAX events and thus needs a DOM?
Judge Maygarden
No, i don't have anything against it, in fact I am using it on another project, however it's big, and for what I need more complex than what i need. so, as i very clearly pointed in my question, I won't have expat as a solution
Jessica
@Jessica You must have your own private definition of the word "big" - Expat is about as small an XML parser as you are going to get. Specifically, it is smaller than Mini-XML.
anon
+1  A: 

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.

code4life
thanks, i already posted this (see my own answer to the question)
Jessica