This is an XML Writer in C++. It doesn't do much but the basics, and I've tested it, so it should hold up well enough. I hope in the near future, I can bring you an XML Reader.
I kinda built the function names after the Visual Basic names for their standard XML Writer (Microsoft's anyways), so if you see the similarities, that's where I got the names from. As for the actual function internals, I don't know if they are the same or not.
XmlWriter.h:
#ifndef XmlWriter_H
#define XmlWriter_H
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
class XmlWriter {
public:
bool open(const std::string);
void close();
bool exists(const std::string);
void writeOpenTag(const std::string);
void writeCloseTag();
void writeStartElementTag(const std::string);
void writeEndElementTag();
void writeAttribute(const std::string);
void writeString(const std::string);
private:
std::ofstream outFile;
int indent;
int openTags;
int openElements;
std::vector<std::string> tempOpenTag;
std::vector<std::string> tempElementTag;
};
#endif
XmlWriter.cpp:
#include "XmlWriter.h"
//=============================================================================
//== Function Name : XmlWriter::exists
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== fileName const std::string The name of the file that is in use
//==
//== Description
//== --------------------------------------------------------------------------
//== This function is used to check if the XML file exists
//=============================================================================
bool XmlWriter::exists(const std::string fileName){
std::fstream checkFile(fileName);
return checkFile.is_open();
}
//=============================================================================
//== Function Name : XmlWriter::open
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== strFile const std::string The name of the file that the user passes
//== in the code
//==
//== Description
//== --------------------------------------------------------------------------
//== This function is used to open the XML file, first checking to see if it
//== exists first
//=============================================================================
bool XmlWriter::open(const std::string strFile) {
if (exists(strFile)){
std::cout << "Error: File alread exists.\n";
return false;
}
outFile.open(strFile);
if (outFile.is_open()) {
std::cout << "File created successfully.\n";
outFile << "<!--XML Document-->\n";
outFile << "<?xml version='1.0' encoding='us-ascii'>\n";
indent = 0;
openTags = 0;
openElements = 0;
return true;
}
return false;
}
//=============================================================================
//== Function Name : XmlWriter::close
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== N/a N/a N/a
//==
//== Description
//== --------------------------------------------------------------------------
//== This function is used to close the XML file
//=============================================================================
void XmlWriter::close() {
if (outFile.is_open()) {
outFile.close();
}
else {
std::cout << "File already closed.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeOpenTag
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== openTag const std::string The name of the tag being created
//==
//== Description
//== --------------------------------------------------------------------------
//== This function creates a new tag, checking that the file is open, and saves
//== the tag name in a vector to keep track of it
//=============================================================================
void XmlWriter::writeOpenTag(const std::string openTag) {
if (outFile.is_open()) {
for (int i = 0; i < indent; i++) {
outFile << "\t";
}
tempOpenTag.resize(openTags + 1);
outFile << "<" << openTag << ">\n";
tempOpenTag[openTags] = openTag;
indent += 1;
openTags += 1;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeCloseTag
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== N/a N/a N/a
//==
//== Description
//== --------------------------------------------------------------------------
//== This function closes the currently open tag
//=============================================================================
void XmlWriter::writeCloseTag() {
if (outFile.is_open()) {
indent -= 1;
for (int i = 0; i < indent; i++) {
outFile << "\t";
}
outFile << "</" << tempOpenTag[openTags - 1] << ">\n";
tempOpenTag.resize(openTags - 1);
openTags -= 1;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeStartElementTag
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== elementTag const std::string The name of the element being created
//==
//== Description
//== --------------------------------------------------------------------------
//== This function creates a new element tag and saves the name to a vector
//=============================================================================
void XmlWriter::writeStartElementTag(const std::string elementTag) {
if (outFile.is_open()) {
for (int i = 0; i < indent; i++) {
outFile << "\t";
}
tempElementTag.resize(openElements + 1);
tempElementTag[openElements] = elementTag;
openElements += 1;
outFile << "<" << elementTag;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeEndElementTag
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== N/a N/a N/a
//==
//== Description
//== --------------------------------------------------------------------------
//== This function closed the currently opened element tag
//=============================================================================
void XmlWriter::writeEndElementTag() {
if (outFile.is_open()) {
outFile << "</" << tempElementTag[openElements - 1] << ">\n";
tempElementTag.resize(openElements - 1);
openElements -= 1;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeAttribute
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== outAttribute const std::string The attribute being written out
//==
//== Description
//== --------------------------------------------------------------------------
//== This function writes an attribute (if any) after the element tag is first
//== opened and before the output for the element is written
//=============================================================================
void XmlWriter::writeAttribute(const std::string outAttribute) {
if (outFile.is_open()) {
outFile << " " << outAttribute;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
//=============================================================================
//== Function Name : XmlWriter::writeString
//==
//== Perameters
//== Name Type Description
//== ---------- ----------- --------------------
//== writeString const std::string The string to be written to the element
//==
//== Description
//== --------------------------------------------------------------------------
//=============================================================================
void XmlWriter::writeString(const std::string outString) {
if (outFile.is_open()) {
outFile << ">" << outString;
}
else {
std::cout << "File is closed. Unable to write to file.\n";
}
}
main.cpp:
#include "XmlWriter.h"
int main() {
XmlWriter xml;
if (xml.open("C:\\Users\\UserNameHere\\Desktop\\test.xml")) {
xml.writeOpenTag("testTag");
xml.writeStartElementTag("testEle1");
xml.writeString("This is my first tag string!");
xml.writeEndElementTag();
xml.writeOpenTag("testTag2");
xml.writeStartElementTag("testEle2");
xml.writeAttribute("testAtt=\"TestAttribute\"");
xml.writeString("I sometimes amaze myself.");
xml.writeEndElementTag();
xml.writeOpenTag("testTag3");
xml.writeStartElementTag("testEle3");
xml.writeAttribute("testAtt2=\"TestAttrib2\"");
xml.writeString("Though i'm sure someone can make something even better");
xml.writeEndElementTag();
xml.writeCloseTag();
xml.writeCloseTag();
xml.writeCloseTag();
xml.close();
std::cout << "Success!\n";
} else {
std::cout << "Error opening file.\n";
}
system("pause");
return 0;
}