42 #include <visp/vpXmlParser.h>
43 #include <visp/vpConfig.h>
47 #include <visp/vpException.h>
48 #include <visp/vpDebug.h>
49 #include <libxml/parser.h>
90 : nodeMap(), main_tag(
"config")
112 if(node ->xmlChildrenNode == NULL){
113 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read char*";
114 std::cerr << errorMsg << std::endl;
117 return (
char *) xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
133 if(node ->xmlChildrenNode == NULL){
134 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read std::string";
135 std::cerr << errorMsg << std::endl;
138 char* dataCh = (
char*)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
139 std::string dataStr = dataCh;
157 if(node ->xmlChildrenNode == NULL){
158 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read int";
159 std::cerr << errorMsg << std::endl;
163 char * control_convert;
166 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
167 val_int = strtol ((
char *)val_char, &control_convert, 10);
169 if (val_char == control_convert){
170 xmlFree((xmlChar*) val_char);
173 xmlFree((xmlChar*) val_char);
191 if(node ->xmlChildrenNode == NULL){
192 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read unsigned int";
193 std::cerr << errorMsg << std::endl;
197 char * control_convert;
198 unsigned int val_uint;
200 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
201 val_uint = strtoul ((
char *)val_char, &control_convert, 10);
203 if (val_char == control_convert){
204 xmlFree((xmlChar*) val_char);
207 xmlFree((xmlChar*) val_char);
226 if(node ->xmlChildrenNode == NULL){
227 std::string errorMsg =
"Empty node " + std::string((
char*)node->name) +
", cannot read double";
228 std::cerr << errorMsg << std::endl;
232 char * control_convert;
235 val_char = (
char *) xmlNodeListGetString(doc, node ->xmlChildrenNode, 1);
236 val_double = strtod ((
char *)val_char, &control_convert);
238 if (val_char == control_convert){
239 xmlFree((xmlChar*) val_char);
242 xmlFree((xmlChar*) val_char);
257 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value);
258 xmlAddChild(node, tmp);
272 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)value.c_str());
273 xmlAddChild(node, tmp);
288 sprintf(str,
"%d", value);
290 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
291 xmlAddChild(node, tmp);
305 sprintf(str,
"%u", value);
307 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
308 xmlAddChild(node, tmp);
323 sprintf(str,
"%lf", value);
325 tmp = xmlNewChild(node, NULL, (xmlChar*)label, (xmlChar*)str);
326 xmlAddChild(node, tmp);
346 xmlNodePtr root_node;
348 doc = xmlParseFile(filename.c_str());
354 root_node = xmlDocGetRootElement(doc);
355 if(root_node == NULL){
379 xmlNodePtr root_node;
381 doc = xmlReadFile(filename.c_str(), NULL, XML_PARSE_NOWARNING + XML_PARSE_NOERROR + XML_PARSE_NOBLANKS);
383 doc = xmlNewDoc ((xmlChar*)
"1.0");
384 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
385 xmlDocSetRootElement(doc, root_node);
390 if (
remove(filename.c_str()) != 0)
393 doc = xmlNewDoc ((xmlChar*)
"1.0");
394 root_node = xmlNewNode(NULL, (xmlChar*)
main_tag.c_str());
395 xmlDocSetRootElement(doc, root_node);
399 root_node = xmlDocGetRootElement(doc);
400 if (root_node == NULL)
408 xmlSaveFormatFile(filename.c_str(), doc, 1);
void xmlWriteIntChild(xmlNodePtr node, const char *label, const int 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)
void xmlWriteStringChild(xmlNodePtr node, const char *label, const std::string &value)
std::map< std::string, int > nodeMap
void parse(const std::string &filename)