XML parser example.
This example contains the declaration of a class used to read and write data in a xml file like:
#include <visp3/core/vpConfig.h>
#include <iostream>
#if defined(VISP_HAVE_XML2)
#include <visp3/core/vpDebug.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpXmlParser.h>
#include <visp3/io/vpParseArgv.h>
#include <string>
#ifndef DOXYGEN_SHOULD_SKIP_THIS
{
protected:
double m_range;
int m_step;
int m_size_filter;
std::string m_name;
typedef enum { config, range, step, size_filter, name } dataToParse;
public:
vpExampleDataParser();
virtual ~vpExampleDataParser();
double getRange() const { return m_range; }
int getStep() const { return m_step; }
int getSizeFilter() const { return m_size_filter; }
std::string getName() const { return m_name; }
void setRange(const double _range) { m_range = _range; }
void setStep(const int _step) { m_step = _step; }
void setSizeFilter(const int _size_filter) { m_size_filter = _size_filter; }
void setName(const std::string &_name) { m_name = _name; }
protected:
};
vpExampleDataParser::vpExampleDataParser() : m_range(0.), m_step(0), m_size_filter(0), m_name("")
{
nodeMap["config"] = config;
nodeMap["range"] = range;
nodeMap["step"] = step;
nodeMap["size_filter"] = size_filter;
nodeMap["name"] = name;
}
vpExampleDataParser::~vpExampleDataParser() {}
void vpExampleDataParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
{
for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
if (dataNode->type == XML_ELEMENT_NODE) {
std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
if (iter_data != nodeMap.end()) {
switch (iter_data->second) {
case range:
break;
case step:
break;
case size_filter:
break;
case name: {
} break;
default:
vpTRACE(
"unknown tag in readConfigNode : %d, %s", iter_data->second, (iter_data->first).c_str());
break;
}
}
}
}
}
void vpExampleDataParser::writeMainClass(xmlNodePtr node)
{
}
#endif // doxygen
#define GETOPTARGS "cdo:h"
void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user);
bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user);
void usage(const char *name, const char *badparam, const std::string &opath, const std::string &user)
{
fprintf(stdout, "\n\
Write and read data in a xml file.\n\
\n\
SYNOPSIS\n\
%s [-o <output image path>] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-o <output data path> %s\n\
Set data output path.\n\
From this directory, creates the \"%s\"\n\
subdirectory depending on the username, where \n\
dataTestXml.xml file is written.\n\
\n\
-h\n\
Print the help.\n\n", opath.c_str(), user.c_str());
if (badparam) {
fprintf(stderr, "ERROR: \n");
fprintf(stderr, "\nBad parameter [%s]\n", badparam);
}
}
bool getOptions(int argc, const char **argv, std::string &opath, const std::string &user)
{
const char *optarg_;
int c;
switch (c) {
case 'o':
opath = optarg_;
break;
case 'h':
usage(argv[0], NULL, opath, user);
return false;
break;
case 'c':
case 'd':
break;
default:
usage(argv[0], optarg_, opath, user);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL, opath, user);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
int main(int argc, const char **argv)
{
try {
std::string opt_opath;
std::string opath;
std::string filename;
std::string username;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << " testXmlParser.cpp" << std::endl << std::endl;
std::cout << " writing and readind data using a xml parser" << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << std::endl;
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
opt_opath = "/tmp";
#elif defined(_WIN32)
opt_opath = "C:\\temp";
#endif
if (getOptions(argc, argv, opt_opath, username) == false) {
exit(-1);
}
if (!opt_opath.empty())
opath = opt_opath;
try {
} catch (...) {
usage(argv[0], NULL, opath, username);
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Cannot create " << dirname << std::endl;
std::cerr << " Check your -o " << opath << " option " << std::endl;
exit(-1);
}
}
{
vpExampleDataParser parser1;
parser1.setRange(3.5);
parser1.setStep(2);
parser1.setSizeFilter(5);
parser1.setName("cube");
std::cout << "Write data to " << filename << std::endl;
parser1.save(filename);
}
{
vpExampleDataParser parser2;
parser2.parse(filename);
std::cout << "Read from " << filename << std::endl;
std::cout << "Range : " << parser2.getRange() << std::endl;
std::cout << "Step : " << parser2.getStep() << std::endl;
std::cout << "Filter size : " << parser2.getSizeFilter() << std::endl;
std::cout << "name : " << parser2.getName() << std::endl;
}
return 0;
std::cout << "Catch an exception: " << e << std::endl;
return 1;
}
}
#else
int main()
{
std::cout << "Xml parser requires libxml2." << std::endl;
return 0;
}
#endif