1

I'm pretty new to RapidXML. I want to construct an Xml document and print it to a file. Everything works but I'm not sure if I'm doing one part of the process right:

Adding an attribute to a node that is a double.

I'm using std c++ stuff:

double value = 1.0;
std::ostringstream strs;
strs << value ;
std::string str = strs.str();
char* numBuff =  doc.allocate_string(str.c_str());
xml_attribute<> *attr = doc.allocate_attribute("name",numBuff);
nodeRef->append_attribute(attr);

Is there a more elegnat/faster way? Something like (wishfull thinking):

double value = 1.0;
char* numBuff =  doc.allocate_string_from_value(value);
xml_attribute<> *attr = doc.allocate_attribute("name",numBuff);

I need to save tons of doubles into my xml file so performance is my key concern here.

Greetings, Oliver

Oliver Zendel
  • 2,695
  • 34
  • 29
  • 2
    If you are just writing to a file, why don't you simply use iostreams and skip the whole creation of the DOM step? – Nim Jan 03 '11 at 09:51
  • Hm, I didn't want to write a (simple) xml printer myself as there are many libraries already in use. RapidXML seems to be the fastest one. I'm using xml instead of a binary/ini file so that everything is extensible and can later be used by other tools. – Oliver Zendel Jan 03 '11 at 15:21

2 Answers2

2

I know that's an old topic, but don't have a conclusive answer. To convert values with more efficient function, prefer the old C sintax (printf), over the C++ streaming, that's more efficient. I implemented by this way:

declare the function for conversion...

char* double2char(double value) {
    char tmpval[64];
    sprintf(tmpval,"%f",value);
    return doc->allocate_string(tmpval);
}

... use in the code ...

double value = 1.0;
xml_attribute<> *attr = doc.allocate_attribute("name",double2char(value));

... That's my implementation,maybe not the best, but a little more elegant and faster...

Best Regards.

ps. Sorry for my the brazilian english.

RFasioli
  • 129
  • 1
  • 7
  • Thanks, I will try out if this improves the performance. As RapidXML currently does not support the kind of operation I was expecting, I will mark your answer as correct and be done with it :) – Oliver Zendel Mar 28 '12 at 11:33
0

Converting numeric types to strings is beyond scope of rapidxml. To do it properly would require a lot more code (and dependencies) than just the xml parser.

kaalus
  • 4,475
  • 3
  • 29
  • 39
  • Well, when using RapidXML to build up a document from scratch, this is a function I would expect and allocate_string must also allocate dynamically so maybe the easiest solution would be to simply add something like allocate_string_from_value with a simple convertion code (similar to my snippet). Maybe a faster code would need to reimplement the conversion parts but that would really take up some code. – Oliver Zendel Aug 17 '11 at 11:50