39 #include <visp3/core/vpConfig.h> 40 #include <visp3/core/vpXmlParser.h> 44 #include <libxml/parser.h> 45 #include <visp3/core/vpDebug.h> 46 #include <visp3/core/vpException.h> 102 if (node->xmlChildrenNode == NULL) {
103 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read char*";
104 std::cerr << errorMsg << std::endl;
107 return (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
122 if (node->xmlChildrenNode == NULL) {
123 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read std::string";
124 std::cerr << errorMsg << std::endl;
127 char *dataCh = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
128 std::string dataStr = dataCh;
146 if (node->xmlChildrenNode == NULL) {
147 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read int";
148 std::cerr << errorMsg << std::endl;
152 char *control_convert;
155 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
156 val_int = (int)strtol((
char *)val_char, &control_convert, 10);
158 if (val_char == control_convert) {
159 xmlFree((xmlChar *)val_char);
162 xmlFree((xmlChar *)val_char);
180 if (node->xmlChildrenNode == NULL) {
181 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read unsigned int";
182 std::cerr << errorMsg << std::endl;
186 char *control_convert;
187 unsigned int val_uint;
189 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
190 val_uint = (
unsigned int)strtoul((
char *)val_char, &control_convert, 10);
192 if (val_char == control_convert) {
193 xmlFree((xmlChar *)val_char);
196 xmlFree((xmlChar *)val_char);
214 if (node->xmlChildrenNode == NULL) {
215 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read double";
216 std::cerr << errorMsg << std::endl;
220 char *control_convert;
223 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
224 val_double = strtod((
char *)val_char, &control_convert);
226 if (val_char == control_convert) {
227 xmlFree((xmlChar *)val_char);
230 xmlFree((xmlChar *)val_char);
247 if (node->xmlChildrenNode == NULL) {
248 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read float";
249 std::cerr << errorMsg << std::endl;
253 char *control_convert;
256 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
257 #if defined(VISP_HAVE_FUNC_STRTOF) 258 val_float = strtof((
char *)val_char, &control_convert);
260 val_float = (float)strtod((
char *)val_char, &control_convert);
263 if (val_char == control_convert) {
264 xmlFree((xmlChar *)val_char);
267 xmlFree((xmlChar *)val_char);
284 if (node->xmlChildrenNode == NULL) {
285 std::string errorMsg =
"Empty node " + std::string((
char *)node->name) +
", cannot read bool";
286 std::cerr << errorMsg << std::endl;
292 val_char = (
char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
293 val_bool = val_char[0] !=
'0';
295 xmlFree((xmlChar *)val_char);
309 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)value);
310 xmlAddChild(node, tmp);
323 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)value.c_str());
324 xmlAddChild(node, tmp);
337 sprintf(str,
"%d", value);
339 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)str);
340 xmlAddChild(node, tmp);
353 sprintf(str,
"%u", value);
355 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)str);
356 xmlAddChild(node, tmp);
369 sprintf(str,
"%lf", value);
371 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)str);
372 xmlAddChild(node, tmp);
385 sprintf(str,
"%f", value);
387 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)str);
388 xmlAddChild(node, tmp);
401 sprintf(str,
"%d", (
int)value);
403 tmp = xmlNewChild(node, NULL, (xmlChar *)label, (xmlChar *)str);
404 xmlAddChild(node, tmp);
424 xmlNodePtr root_node;
426 doc = xmlParseFile(filename.c_str());
432 root_node = xmlDocGetRootElement(doc);
433 if (root_node == NULL) {
456 xmlNodePtr root_node;
458 doc = xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOWARNING + XML_PARSE_NOERROR + XML_PARSE_NOBLANKS);
460 doc = xmlNewDoc((xmlChar *)
"1.0");
461 root_node = xmlNewNode(NULL, (xmlChar *)
main_tag.c_str());
462 xmlDocSetRootElement(doc, root_node);
466 if (
remove(filename.c_str()) != 0)
469 doc = xmlNewDoc((xmlChar *)
"1.0");
470 root_node = xmlNewNode(NULL, (xmlChar *)
main_tag.c_str());
471 xmlDocSetRootElement(doc, root_node);
475 root_node = xmlDocGetRootElement(doc);
476 if (root_node == NULL) {
483 xmlSaveFormatFile(filename.c_str(), doc, 1);
487 #elif !defined(VISP_BUILD_SHARED_LIBS) 490 void dummy_vpXmlParser(){};
void xmlWriteIntChild(xmlNodePtr node, const char *label, const int value)
void xmlWriteFloatChild(xmlNodePtr node, const char *label, const float value)
void xmlWriteBoolChild(xmlNodePtr node, const char *label, const bool value)
error that can be emited by ViSP classes.
virtual void writeMainClass(xmlNodePtr node)=0
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
char * xmlReadCharChild(xmlDocPtr doc, xmlNodePtr node)
void save(const std::string &filename, const bool append=false)
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
void xmlWriteUnsignedIntChild(xmlNodePtr node, const char *label, const unsigned int value)
void xmlWriteDoubleChild(xmlNodePtr node, const char *label, const double value)
void xmlWriteCharChild(xmlNodePtr node, const char *label, const char *value)
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)=0
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
std::string xmlReadStringChild(xmlDocPtr doc, xmlNodePtr node)
float xmlReadFloatChild(xmlDocPtr doc, xmlNodePtr node)
void xmlWriteStringChild(xmlNodePtr node, const char *label, const std::string &value)
bool xmlReadBoolChild(xmlDocPtr doc, xmlNodePtr node)
std::map< std::string, int > nodeMap
void parse(const std::string &filename)