CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    parse a .xml with just C++ Standard Library

    How can I parse an .xml file with just C++ Standard Library? I hope you can tell me a hint or simple guidance.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: parse a .xml with just C++ Standard Library

    Write an XML parser, assuming of the content of that .xml is an XML document. If the XML document has a particularly fixed structure, perhaps other approaches could be used, e.g., a simple search for a string, regular expressions, etc.

    Why not ditch the restriction and use an existing XML parser library?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: parse a .xml with just C++ Standard Library

    Why not ditch the restriction and use an existing XML parser library?
    that is the assignment.

    The file is .xml. So I have to use fstream and parse each word individualy or something like that. Do you have an example program of this kind?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: parse a .xml with just C++ Standard Library

    I have to use fstream and parse each word individually or something like that
    Writing a complete xml parser is non-trivial and for an assignment seems somewhat excessive. What exactly is the assignment?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,100

    Re: parse a .xml with just C++ Standard Library

    Quote Originally Posted by flex567
    How can I parse an .xml file with just C++ Standard Library? I hope you can tell me a hint or simple guidance.
    I agree with laserlight and 2kaud.
    However, have a look at
    http://www.codeproject.com/Articles/...r-in-Cplusplus
    and some other parsers in http://www.codeproject.com/KB/recipes/#Parsers
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: parse a .xml with just C++ Standard Library

    to parse data from an .xml document.

    example of a document

    Pretty much all the lines are like the ones below
    <tecajnica datum="2007-01-01">
    <tecaj sifra="840" oznaka="USD">1.3170</tecaj>
    <tecaj sifra="392" oznaka="JPY">156.93</tecaj>
    ...
    So I guess I have to do it with fstream? Do you have an example program that shows how to do that with fstream?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: parse a .xml with just C++ Standard Library

    Quote Originally Posted by flex567
    to parse data from an .xml document.

    example of a document

    Pretty much all the lines are like the ones below
    Are there no restrictions, simplifications, etc? Are you supposed to parse an arbitrary XML document into a DOM tree or write a SAX parser, or is your task more specific, e.g., parse an XML document containing historical currency conversion data and display the currency conversion rates in a series of tables?

    Quote Originally Posted by flex567
    So I guess I have to do it with fstream? Do you have an example program that shows how to do that with fstream?
    Yes, it makes sense to use fstream. Don't you already know how to use fstream? If you really have to write a parser for arbitrary XML documents, then the usage of fstream is easy: figuring out how the parsing should work then implementing it is the hard part. For that you would do things like research on parsing algorithms, download code for existing parsers to see how they work (like the starting points given in post #5), etc.
    Last edited by laserlight; March 11th, 2015 at 07:00 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: parse a .xml with just C++ Standard Library

    I know how to use fstream but an example program that parses with fstream would be great.


    Are there no restrictions, simplifications, etc? Are you supposed to parse an arbitrary XML document into a DOM tree or write a SAX parser, or is your task more specific, e.g., parse an XML document containing historical currency conversion data and display the currency conversion rates in a series of tables?
    I should write a function that calculates the difference between the chosen course and the one in a file on a specific date.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,100

    Re: parse a .xml with just C++ Standard Library

    Did you look at the codeproject site?
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: parse a .xml with just C++ Standard Library

    A generic parser than can parse an xml in all of it's possible forms, encodings, with namespaces, DTD's and custom entities etc... is a MASSIVE job, and even high profile libraries don't (properly) handle all the nuances correctly.
    And then we aren't even (yet) talking about doing this efficiently both in memory usage and performance. We're easily talking several thousands lines of code... and more depending on how you intend to extract the parsed data (DOM, SAX, XPath, data binding ?).

    Now, if you mean "I need to parse a very specific kind of xml", then you may be able to get away with a simplified match and extract type parser.
    if you only need to deal with the type of structure as you describe in #6, then:
    read the xml line per line
    use a regex to verify the line matches one of the patterns you want to support, and extract get the data you need (you can do both at the same time with regex)
    or write your own simple parser to "does it start with X and end with Y, then extra all the stuff between X and Y"


    and just to point it out for the future generation. regex CAN NOT be used as a basis for a generic xml parser. regex only handles context-free regular languages, and xml is neither a context-free language nor a regular language.
    You can use regex for specific types of xml and for subdomains of xml parsing, but not as a basis for a whole parser.

  11. #11
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: parse a .xml with just C++ Standard Library

    Did you look at the codeproject site?
    I just found an interesting program on that site.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: parse a .xml with just C++ Standard Library

    Quote Originally Posted by flex567
    I should write a function that calculates the difference between the chosen course and the one in a file on a specific date.
    Ah, so you are working with a file with a specific XML format, not with arbitrary XML documents. What is the format of this file? What is a "chosen course", what is the date format, etc? You need to be clear on these requirements.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: parse a .xml with just C++ Standard Library

    I think I first have to figure out what exactly should this program do.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,553

    Re: parse a .xml with just C++ Standard Library

    Quote Originally Posted by flex567 View Post
    I think I first have to figure out what exactly should this program do.

    That would be a good start
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: parse a .xml with just C++ Standard Library

    If you can assume
    - a fixed encoding (like only need to support utf8) or limited set of encodings.
    - you do not need support for DTD's, custom entities and namespaces.
    - you can assume that the xml's will either be correct and valid or can be considered "faulty" with no form of correction/fallback (it's good and parsed or it's invalid)
    - only need to read and not write the xml...

    then you can make a simple xml parser supporting "any xml" that matches the above, with element-level access in just under 800 lines of code.

    I needed something like this for a project a few years back where I couldn't use a lib due to memory constraints, so I rolled my own parser. corporate rules prevent me from posting it here unless I get it "declassified" first, which is a bit of a hassle.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured