Newbie: parsing simple XML with C/C++

Discussion in 'XML' started by Steven Feil, Oct 29, 2003.

  1. Steven Feil

    Steven Feil Guest

    I'm looking for simple examples of XML parsing using C/C++ that could
    be applicable to both Unix and Windows programming. I am wanting to
    parse an XML structure that is basically flat. The Information that I
    wish to extract would be held within XML attributes. It is important
    that the order of the document be preserved in the parsing process.


    Here is a document similar to the type of XML that will be generated.

    <collector name="John Tomas">
    <cd title="Moon Light City" artist="Fred Ziffle" />
    <book title="Hiking Big Bend" author="Laurence Parent" pages="171" />
    <cd title="Ups and Downs" artist="Hank Kimble" />
    <cd title="Eat My Shorts" artist="Bart Simpson" />
    <book title="100 things to cook" author="Tom Duly" pages="100" />
    </collector>

    My program would need to know what order the information was
    in. Specifically the cd "Moon Light City" was found first followed by
    the book "Hiking Big Bend" and so forth. I would also need to extract
    all the data, noting that the data for a cd is different than that for
    a book.

    I've looked into xpath, but what I've read about it so far seams much
    more complicated than what I need. Most of the XML books I've seen
    seem to concentrate on the DTD and interoperability between HTML and
    XML. I've written a DTD that I believe is right, but I haven't tested
    it yet.

    Any websites or steering on what I should search the Internet for
    would be greatly appreciated.
     
    Steven Feil, Oct 29, 2003
    #1
    1. Advertising

  2. Steven Feil

    strajan Guest

    You can download xerces parser from apache, which comes with samples on
    XMLparsing.
    http://xml.apache.org/xerces-c/index.html

    HTH

    "Steven Feil" <> wrote in message
    news:p...
    > I'm looking for simple examples of XML parsing using C/C++ that could
    > be applicable to both Unix and Windows programming. I am wanting to
    > parse an XML structure that is basically flat. The Information that I
    > wish to extract would be held within XML attributes. It is important
    > that the order of the document be preserved in the parsing process.
    >
    >
    > Here is a document similar to the type of XML that will be generated.
    >
    > <collector name="John Tomas">
    > <cd title="Moon Light City" artist="Fred Ziffle" />
    > <book title="Hiking Big Bend" author="Laurence Parent" pages="171" />
    > <cd title="Ups and Downs" artist="Hank Kimble" />
    > <cd title="Eat My Shorts" artist="Bart Simpson" />
    > <book title="100 things to cook" author="Tom Duly" pages="100" />
    > </collector>
    >
    > My program would need to know what order the information was
    > in. Specifically the cd "Moon Light City" was found first followed by
    > the book "Hiking Big Bend" and so forth. I would also need to extract
    > all the data, noting that the data for a cd is different than that for
    > a book.
    >
    > I've looked into xpath, but what I've read about it so far seams much
    > more complicated than what I need. Most of the XML books I've seen
    > seem to concentrate on the DTD and interoperability between HTML and
    > XML. I've written a DTD that I believe is right, but I haven't tested
    > it yet.
    >
    > Any websites or steering on what I should search the Internet for
    > would be greatly appreciated.
     
    strajan, Oct 29, 2003
    #2
    1. Advertising

  3. In article <>,
    Steven Feil <> wrote:

    % I'm looking for simple examples of XML parsing using C/C++ that could
    % be applicable to both Unix and Windows programming.

    You should use an XML parser, rather than trying to write code that does
    the parsing. There are a bunch of them out there -- my current favourite
    is libxml (http://xmlsoft.org), but my old favourite (expat -- don't
    have a URL) works well and is a little lighter-weight. I didn't like
    using xerces, from the apache project, partly because I find the name
    irritating, and partly because I was doing some non-standard things with
    it and ran into problems with version changes.

    There are two styles of parser interfaces. You can write a bunch of
    callback functions which get passed bits of the file as they're parsed
    and do what you want with them, or you can have the parser give you a
    tree structure, then have your code walk the tree when it wants to get
    at the data. Expat originally supported the first style of interface
    only, while most of the more recent parsers support both styles. I believe
    there's an expat project on source forge which has likely moved the
    interface forward a bit from the version I use.

    % I've looked into xpath, but what I've read about it so far seams much
    % more complicated than what I need.

    It depends on what you need to do with the data. If your goal is to
    find a specific CD, say, XPath can save you a lot of coding. If you
    look at all the data, it doesn't really give you anything.

    % Most of the XML books I've seen
    % seem to concentrate on the DTD and interoperability between HTML and
    % XML. I've written a DTD that I believe is right, but I haven't tested
    % it yet.

    You don't need a DTD, although it doesn't hurt to define your data
    model up front, and the DTD is a reasonable way of documenting it.

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Oct 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Example needed: simple XML file and parsing

    VB Programmer, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    822
    Craig Deelsnyder
    Jan 6, 2004
  2. John Levine
    Replies:
    0
    Views:
    767
    John Levine
    Feb 2, 2012
  3. John
    Replies:
    1
    Views:
    133
  4. John
    Replies:
    1
    Views:
    100
  5. Erik Wasser
    Replies:
    5
    Views:
    530
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page