A. Hue A. Hue - 1 year ago 69
C++ Question

Easy way to validate XML File

I have a C++ program, where the user can pic a .xml file in which tracks are stored. Basically these files are .gpx files with a minor change so the pugixml can read it. How can I make sure that the file the user selected has the right format? It is for example important, that the

Track x
and that the node names are right.
This is an example of such a file:

<?xml version='1.0' encoding='UTF-8'?>
<name>Track 1</name>
<trkpt lat="48.08162894291554" lon="11.274891365644537">
<trkpt lat="48.08167553246409" lon="11.274872590181431">
<name>Track 2</name>
<trkpt lat="48.08162939089216" lon="11.274952385899628">

Is there an easy way to validate the form of an .xml file? Or do I have to check manually?

Answer Source

I know two ways to validate xml files:

Both validation methods should be handled by the parser. Which method to choose depends on the level of granularity you need for validation, whereby XSD (Schema) is more sophisticated and on what is available for you. So if there is a DTD (Doctype Definition) is available, is might be more "easy" to just use that. You can of course write some code which does the validation, too, but I guess that is not the "easy way" you are looking for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download