3

I have an configuration xml file which has some values like

<config>
  <map>100,1,200,1</map>
  <image>abc.bmp</image>
  . 
  .
  .
  .
</config>

etc.

I imported the file read line by line all are done. I have to validate the fields in file. Like

1.  <map> " "</map> is not empty,no junk value, 
2.  <image>abc**,**bmp</im*E*ge> (spelling mistake)
3.  <image>abc.bmp </config> ( missing tags)

I have to develop a unique algorithm so that cant use libs . Is there any idea rather than loading and checking every one character by character?

  • 2
    _'I have to develop a unique algorithm so that cant use libs'_ can you elaborate on this? I would highly recommend to use a XML parsing library, it's too error prone to implement this yourself. There are a number of libs you can also use in commercial projects without problems. – πάντα ῥεῖ Jan 07 '13 at 10:11
  • @ g-makulik : I have to add this in a firmware code later. so i think using libs may cause some issues(don't sure). –  Jan 07 '13 at 10:37
  • If you're concerned about impact (code/memory usage) of an external lib, you might check [expat](http://expat.sourceforge.net/) or [tinyxml](http://www.grinninglizard.com/tinyxml/). Have a look at the requirements and licenses to decide if they can be integrated to your firmware. – πάντα ῥεῖ Jan 07 '13 at 10:42
  • Thanks g-makulik. I think that will provide something that will be useful. –  Jan 07 '13 at 10:49
  • You mention to read the config from a file. Are you using an OS (e.g. linux) on your target? In this case integration should be possible w.o. any problems. – πάντα ῥεῖ Jan 07 '13 at 10:52
  • Yes as a sample code i am using in windows. Final code not in an os. –  Jan 07 '13 at 11:56
  • What did you read the file into, an array? – ChiefTwoPencils Jan 07 '13 at 22:10
  • i found this [link](http://stackoverflow.com/questions/5443073/read-a-line-from-xml-file-using-c) . –  Jan 08 '13 at 05:56

1 Answers1

0

I'd recommend to use a 3rd party library to implement XML parsing. Getting all the details and pitfalls of XML parsing correct is much harder than you might think.

Your points 2. and 3. will be supported well by any complete XML parser. Point 1. will need either XSL schema definition and a parser that supports schema validation, or you'll need to provide extra validation code manually.

If you're concerned about impact (code/memory usage) you should refer to these lightweight C/C++ XML parsers:

  • C++ expat (can be used in commercial projects)
  • TinyXml (can be used in commercial projects)

Other XML parsers

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190