I have to parse an XML file in C++. I was researching and found the RapidXml library for this.
I have doubts about doc.parse<0>(xml)
.
Can xml
be an .xml file or does it need to be a string
or char *
?
If I can only use string
or char *
then I guess I need to read the whole file and store it in a char array and pass the pointer of it to the function?
Is there a way to directly use a file because I would need to change the XML file inside the code also.
If that is not possible in RapidXml then please suggest some other XML libraries in C++.
Thanks!!!
Ashd
The manual tells us:
RapidXML leaves loading the character data from a file to you. Either read the file into a buffer, like anno suggested or alternatively use some memory mapping technique. (But look up
parse_non_destructive
flag first.)RapidXml comes with a class to do this for you,
rapidxml::file
in therapidxml_utils.hpp
file. Something like:Note that the
xmlFile
object now contains all of the data for the XML, which means that once it goes out of scope and is destroyed the doc variable is no longer safely usable. If you call parse inside of a function, you must somehow retain thexmlFile
object in memory (global variable, new, etc) so that the doc remains valid.New to C++ myself... but I wanted to share a solution.
YMMV!
Shout Out to SiCrane on this thread: -- and just replacing 'string' with a vector --- (thanks anno)
Please comment and help me learn also! I'm very new to this
Anyway, this seems to work for a good start:
We usually read the XML from the disk into a
std::string
, then make a safe copy of it into astd::vector<char>
as demonstrated below:For a complete source code check:
Read a line from xml file using C++