I have created a binary file with 100,000 Employee records in C++ (GNU compiler). Now I need to create XML table with that 100,000 Employee Records using c++. But I don't know how to create XML table using C++ code. Is there any sample code or tutorial available to do this program ?
Asked
Active
Viewed 803 times
0
-
1You should pick an XML library and work through the tutorials. There are lots of questions about "best XML library", my 10 seconds searching dug happened out a Windows-centric one that may or may not help you - http://stackoverflow.com/questions/2990903/best-xml-library-in-c-fast-set-up - but I suggest you search a bit more if you're on another OS. Resist the temptation to code it yourself unless you're prepared to make sure you're using the right value escaping conventions. – Tony Delroy Jun 23 '12 at 05:20
-
There is nothing special or magical about XML it is just text surrounding your data. – Adrian Cornish Jun 23 '12 at 05:40
-
possible duplicate of [What XML parser should I use in C++?](http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c) – Nicol Bolas Jun 23 '12 at 05:41
-
@NicolBolas - I disagree - the OP does not need to parse XML so no library is need. XML is just fancy delimiters – Adrian Cornish Jun 23 '12 at 06:01
-
@AdrianCornish: as I said above, values you put into attributes and elements need certain characters (e.g. &, <) specially encoded. Your "answer" ignores that. – Tony Delroy Jun 23 '12 at 06:38
-
I said it was contrived and really - you need a whole library just to encode a couple of chars - How many people to you know with an ampersand in their name ;-). Code it when you need it - not just because you might. – Adrian Cornish Jun 23 '12 at 06:42
-
@Adrian: "Code it when you need it - not just because you might." It depends. If downloading and using a simple, easy-to-use library can make these kinds of problems go away even if you would never encounter them, then I disagree. Also, needs change; if you suddenly start putting characters that need to be escaped in there, then your previously "working" code breaks. But if you did your job right the first time, everything works out as normal. – Nicol Bolas Jun 23 '12 at 06:45
-
@Nicol - have you ever created an XML document with a simple to use library? Lets see libxml2 - feature rich - pain in the arse to use - xerces - feature rich - pain in the arse to use. Why do people think XML is hard/its own language or complicated in any way shape or form - writing XML is easy - parsing it not so much. Its just delimiters. If I want to output CSV - yes I have to check for comma's in my data - do I need a 3rd party library to do that - no. Many inexperienced programmers spend too much time coding for every possibility - for code that will likely never run – Adrian Cornish Jun 23 '12 at 06:51
-
@Adrian: The fact that you only cite two XML libraries (the two that implement 100% of the XML spec and therefore earn that complexity) suggests that you haven't really sampled the gamut of what's available. There's a reason I wrote that question and answer; it's intended to be comprehensive of what is available for XML work. And it's more than just two libraries. Also, while you might not *need* a 3rd party library to write CSV, if such a library exists, I would certainly use it rather than write my own. XML is no different in that regard. – Nicol Bolas Jun 23 '12 at 06:57
-
@NicolBolas We could argue all night and never agree - my point was simply that most likely for what the OP wants a library is not needed and could be done with simple markup tags :-) It becomes a matter of personal preference if you want to use a library that implements the square() function or handroll it yourself. – Adrian Cornish Jun 23 '12 at 07:00
-
Unless you clearly understand the pitfalls of hand-rolling your own XML library and understand the tradeoffs that you are making by using it - go with a tried and tested library - XML is a complicated beast, and its easy to get caught by an edge case. – Michael Anderson Jun 23 '12 at 07:51
-
@AdrianCornish: "how many people do you know with an ampersand in their name"... how many users could type an ampersand into a name when using your program accidentally, or just for the pleasure of seeing the generated XML corrupted and the application and any downstream systems fail? Are you going to remember to start encoding fields properly when you add the first one that's meant to allow some otherwise illegal characters? Do you know what they all are to avoid doing it unknowingly? Will every maintenance programmer know? – Tony Delroy Jun 23 '12 at 16:19
2 Answers
0
Here is simple contrived example
#include <iostream>
#include <vector>
#include <string>
class Employee
{
public:
Employee(const std::string &firstname, const std::string &lastname, int salary)
:firstname_(firstname), lastname_(lastname), salary_(salary)
{
}
friend std::ostream &operator<<(std::ostream &os, const Employee &rhs)
{
rhs.print(os);
return os;
}
private:
void print(std::ostream &os) const
{
os << "<employee>";
os << "<firstname>" << firstname_ << "</firstname>";
os << "<lastname>" << lastname_ << "</lastname>";
os << "<salary>" << salary_ << "</salary>";
os << "</employee>\n";
}
std::string firstname_;
std::string lastname_;
int salary_;
};
int main(int argc, char *argv[])
{
std::vector<Employee> staff;
staff.push_back(Employee("Peter", "Griffin", 10000));
staff.push_back(Employee("Lois", "Griffin", 20000));
staff.push_back(Employee("Chris", "Griffin", 30000));
staff.push_back(Employee("Meg", "Griffin", 40000));
staff.push_back(Employee("Stewie", "Griffin", 50000));
staff.push_back(Employee("Brian", "Griffin", 60000));
std::cout << "<staff>\n";
for(std::vector<Employee>::const_iterator i=staff.begin(),end=staff.end(); i!=end; ++i)
{
std::cout << (*i);
}
std::cout << "</staff>\n";
return 0;
}

Adrian Cornish
- 23,227
- 13
- 61
- 77
-
Just as an aside - whoever down voted this please explain how this is wrong ;-) – Adrian Cornish Jun 23 '12 at 07:06
-
1I wasn't the down voter, but this doesn't correctly escape any of the fields - which means you're opening your self to a whole raft of issues. It also doesn't pay any attention to the "encoding" of the XML. – Michael Anderson Jun 23 '12 at 07:48
0
I recommend using an XML serialization library for writing your data into a custom XML format.
For instance the open source, MIT-licensed C++ library libstudxml provides both a low-level API
void start_element (const std::string& name);
void end_element ();
void start_attribute (const std::string& name);
void end_attribute ();
void characters (const std::string& value);
and a high-level API
template <typename T>
void element (const T& value);
template <typename T>
void characters (const T& value);
template <typename T>
void attribute (const std::string& name,
const T& value);
The libstudxml documentation mentions that its serialization source code has its origin in a small C library for XML serialization called Genx (also MIT licensed).

Erik Sjölund
- 10,690
- 7
- 46
- 74