A. Hue A. Hue - 21 days ago 5
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

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

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


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

Answer

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.

Comments