Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an XML file with RapidXml

Tags:

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

like image 222
ashd Avatar asked May 11 '10 04:05

ashd


People also ask

How many ways we can parse XML file?

Java provides many ways to parse an XML file. There are two parsers in Java which parses an XML file: Java DOM Parser. Java SAX Parser.

What is parsing XML?

Definition. XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.

What is RapidXml?

RapidXml is an attempt to create the fastest XML parser possible, while retaining useability, portability and reasonable W3C compatibility. It is an in-situ parser written in modern C++, with parsing speed approaching that of strlen function executed on the same data.


1 Answers

RapidXml comes with a class to do this for you, rapidxml::file in the rapidxml_utils.hpp file. Something like:

#include "rapidxml_utils.hpp"  int main() {     rapidxml::file<> xmlFile("somefile.xml"); // Default template is char     rapidxml::xml_document<> doc;     doc.parse<0>(xmlFile.data()); ... } 

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 the xmlFile object in memory (global variable, new, etc) so that the doc remains valid.

like image 73
Superfly Jon Avatar answered Oct 11 '22 11:10

Superfly Jon