Parsing a xml file

Hi to all,
i want c++ code for parsing a simple xml file.

OR

C++ code for it takes one any text file or xml file from user and it have to eliminate the TOKENS in that file and display the plain text in that file. Please help me. Thank you..
vineela wrote:
i want c++ code
And I want a cheese and ham sandwich, but I'm on a diet :p

Have you got any code that you've written that you could post?

Have you tried looking at some tutorials? I did an similar application a few years ago, I'll detail the steps for you and if you get stuck on any parts you can ask again.

1) Read in the XML file to a string.

String Class: http://www.cplusplus.com/reference/string/string/
File I/O: http://www.cplusplus.com/doc/tutorial/files/

2) Read the string one character at a time.

Getting a Character from a String object: http://www.cplusplus.com/reference/string/string/at/

3) When you find an opening angle bracket "<", treat all the characters you find before the closing angle bracket ">" as a tag and store it (possibly in an array of strings).

To Compare String Objects: http://www.cplusplus.com/reference/string/string/compare/

4) When you find the closing angle bracket you can use the stored tag name and compare it against various predefined tags, to perform some kind of unique behavior.

5) If you want you can check that it has a closing tag, </tag>.

If you get stuck on any of these, feel free to post your code.

Good luck.
Last edited on
Google "c++ xml" and you'll get some good hits on libraries you can use.
I assume this is an exercise? So you need to write your own parser?

If so, I pretty much agree with Ink2019. But I would consider using the string class's find method to find the key chars, rather than walking the string a character at a time.

P.S. Re-reading the original post, to check I had written sense, I see the problem is simpler than I first thought. Based on the second restatement of the problem.

You need to filter the file rather than actually/fully parse it. Given that, I think there's a simpler (lazier!) solution that avoids the use of the string class completely. But the wording of the problem is a little unclear wrt display vs updating the file. Do you just need to display the file (without tags)?
Last edited on
I have used TinyXml to read, alter and save xml files.

http://sourceforge.net/projects/tinyxml/
I like TinyXml myself. As andywestken pointed out, however, this is really a tokenizing problem.

Open file.
While there is data:
  For each character != <, print it.*
  For each character != >, ignore it.

* There are special escapes that you need to handle as well:
  &lt --> <
  &gt --> >
  &amp --> &
  &apos --> '
  &quot --> "

This is a really simplistic version -- there are some other issues which may be encountered, but that should work for all XML your teacher will throw at you.
Topic archived. No new replies allowed.