Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I recently posted about a basic XML writer basic XML writer in C++ and got a lot of great feedback. Well, I'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

I recently posted about a basic XML writer in C++ and got a lot of great feedback. Well, I'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

I recently posted about a basic XML writer in C++ and got a lot of great feedback. Well, I'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

added 44 characters in body
Source Link
CodeMonkey
  • 451
  • 1
  • 4
  • 12
#include <Windows.h>
#include <lmcons.h>
#include "XmlWriter.h"
XmlWriter::XmlWriter() {
 char username[UNLEN + 1];
 DWORD username_len = UNLEN + 1;
 GetUserName(username, &username_len);
 std::string strUserName(username);
 std::string fileName = "C:\\Users\\" + strUserName + "\\My Documents\\DefaultXml.xml";
 std::string xmlEncoding = "utf-8";
 
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "Default File Exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName) {
 std::string xmlEncoding = "utf-8";
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 }
 else {
 std::cerr << "File already exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName, std::string xmlEncoding) {
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "File already exists.";
 }
}
void XmlWriter::writeStartDocument() {
 if (!startDocument) {
 startDocument = true;
 docWrite = true;
 }
}
void XmlWriter::writeEndDocument() {
 if (startDocument) {
 startDocument = false;
 docWrite = false;
 }
}
bool XmlWriter::exists(std::string fileName){
 std::fstreamchar checkFile*outFile = (char*)fileName.c_str();
 returnstd::ifstream checkFile.is_open(outFile);
 return !!checkFile;
}
bool XmlWriter::isOpen() {
 if (outFile.is_open()) {
 return true;
 }
 return false;
}
void XmlWriter::close() {
 if (!startDocument) {
 outFile.close();
 }
}
void XmlWriter::writeStartElement(std::string elementTag) {
 if (startDocument) {
 outFile << "<!--XML Document-->\n";
 outFile << "<?xml version='1.0' encoding='" << xmlEncode << "'?>";
 startDocument = false;
 }
 if (docWrite) {
 if (firstStartElement) {
 firstStartElement = false;
 }
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 current_indent++;
 tagStack.push(elementTag);
 outFile << "<" << tagStack.top() << ">";
 elementOpen = true;
 stringWritten = false;
 }
}
void XmlWriter::writeEndElement() {
 if (docWrite) {
 if (!(tagStack.isEmptyStack())) {
 if (!firstStartElement) {
 outFile << "</" << tagStack.top() << ">";
 current_indent--;
 firstStartElement = true;
 } else {
 current_indent--;
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 outFile << "</" << tagStack.top() << ">";
 }
 tagStack.pop();
 elementOpen = false;
 stringWritten = false;
 } else {
 std::cerr << "No tags to close.";
 }
 } 
}
void XmlWriter::writeAttribute(std::string outAttribute) {
 if (docWrite && elementOpen && !stringWritten) {
 long pos = outFile.tellp();
 outFile.seekp(pos - 1);
 outFile << " " << outAttribute << ">";
 }
}
void XmlWriter::writeString(std::string outString) {
 if (docWrite && elementOpen) {
 outFile << outString;
 stringWritten = true;
 }
}
#include <Windows.h>
#include <lmcons.h>
#include "XmlWriter.h"
XmlWriter::XmlWriter() {
 char username[UNLEN + 1];
 DWORD username_len = UNLEN + 1;
 GetUserName(username, &username_len);
 std::string strUserName(username);
 std::string fileName = "C:\\Users\\" + strUserName + "\\My Documents\\DefaultXml.xml";
 std::string xmlEncoding = "utf-8";
 
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "Default File Exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName) {
 std::string xmlEncoding = "utf-8";
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 }
 else {
 std::cerr << "File already exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName, std::string xmlEncoding) {
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "File already exists.";
 }
}
void XmlWriter::writeStartDocument() {
 if (!startDocument) {
 startDocument = true;
 docWrite = true;
 }
}
void XmlWriter::writeEndDocument() {
 if (startDocument) {
 startDocument = false;
 docWrite = false;
 }
}
bool XmlWriter::exists(std::string fileName){
 std::fstream checkFile(fileName);
 return checkFile.is_open();
}
bool XmlWriter::isOpen() {
 if (outFile.is_open()) {
 return true;
 }
 return false;
}
void XmlWriter::close() {
 if (!startDocument) {
 outFile.close();
 }
}
void XmlWriter::writeStartElement(std::string elementTag) {
 if (startDocument) {
 outFile << "<!--XML Document-->\n";
 outFile << "<?xml version='1.0' encoding='" << xmlEncode << "'?>";
 startDocument = false;
 }
 if (docWrite) {
 if (firstStartElement) {
 firstStartElement = false;
 }
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 current_indent++;
 tagStack.push(elementTag);
 outFile << "<" << tagStack.top() << ">";
 elementOpen = true;
 stringWritten = false;
 }
}
void XmlWriter::writeEndElement() {
 if (docWrite) {
 if (!(tagStack.isEmptyStack())) {
 if (!firstStartElement) {
 outFile << "</" << tagStack.top() << ">";
 current_indent--;
 firstStartElement = true;
 } else {
 current_indent--;
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 outFile << "</" << tagStack.top() << ">";
 }
 tagStack.pop();
 elementOpen = false;
 stringWritten = false;
 } else {
 std::cerr << "No tags to close.";
 }
 } 
}
void XmlWriter::writeAttribute(std::string outAttribute) {
 if (docWrite && elementOpen && !stringWritten) {
 long pos = outFile.tellp();
 outFile.seekp(pos - 1);
 outFile << " " << outAttribute << ">";
 }
}
void XmlWriter::writeString(std::string outString) {
 if (docWrite && elementOpen) {
 outFile << outString;
 stringWritten = true;
 }
}
#include <Windows.h>
#include <lmcons.h>
#include "XmlWriter.h"
XmlWriter::XmlWriter() {
 char username[UNLEN + 1];
 DWORD username_len = UNLEN + 1;
 GetUserName(username, &username_len);
 std::string strUserName(username);
 std::string fileName = "C:\\Users\\" + strUserName + "\\My Documents\\DefaultXml.xml";
 std::string xmlEncoding = "utf-8";
 
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "Default File Exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName) {
 std::string xmlEncoding = "utf-8";
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 }
 else {
 std::cerr << "File already exists.";
 }
}
XmlWriter::XmlWriter(std::string fileName, std::string xmlEncoding) {
 if (!(exists(fileName))) {
 outFile.open(fileName);
 if (outFile.is_open()) {
 std::cout << "File created successfully.\n";
 current_indent = 0;
 startDocument = false;
 docWrite = false;
 firstStartElement = true;
 elementOpen = false;
 stringWritten = false;
 xmlEncode = xmlEncoding;
 }
 } else {
 std::cerr << "File already exists.";
 }
}
void XmlWriter::writeStartDocument() {
 if (!startDocument) {
 startDocument = true;
 docWrite = true;
 }
}
void XmlWriter::writeEndDocument() {
 if (startDocument) {
 startDocument = false;
 docWrite = false;
 }
}
bool XmlWriter::exists(std::string fileName){
 char *outFile = (char*)fileName.c_str();
 std::ifstream checkFile(outFile);
 return !!checkFile;
}
bool XmlWriter::isOpen() {
 if (outFile.is_open()) {
 return true;
 }
 return false;
}
void XmlWriter::close() {
 if (!startDocument) {
 outFile.close();
 }
}
void XmlWriter::writeStartElement(std::string elementTag) {
 if (startDocument) {
 outFile << "<!--XML Document-->\n";
 outFile << "<?xml version='1.0' encoding='" << xmlEncode << "'?>";
 startDocument = false;
 }
 if (docWrite) {
 if (firstStartElement) {
 firstStartElement = false;
 }
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 current_indent++;
 tagStack.push(elementTag);
 outFile << "<" << tagStack.top() << ">";
 elementOpen = true;
 stringWritten = false;
 }
}
void XmlWriter::writeEndElement() {
 if (docWrite) {
 if (!(tagStack.isEmptyStack())) {
 if (!firstStartElement) {
 outFile << "</" << tagStack.top() << ">";
 current_indent--;
 firstStartElement = true;
 } else {
 current_indent--;
 outFile << "\n";
 outFile << std::string(current_indent, '\t');
 outFile << "</" << tagStack.top() << ">";
 }
 tagStack.pop();
 elementOpen = false;
 stringWritten = false;
 } else {
 std::cerr << "No tags to close.";
 }
 } 
}
void XmlWriter::writeAttribute(std::string outAttribute) {
 if (docWrite && elementOpen && !stringWritten) {
 long pos = outFile.tellp();
 outFile.seekp(pos - 1);
 outFile << " " << outAttribute << ">";
 }
}
void XmlWriter::writeString(std::string outString) {
 if (docWrite && elementOpen) {
 outFile << outString;
 stringWritten = true;
 }
}
edited body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I recently posted about a basic XML writer in C++ and got a lot of great feedback. Well, i'mI'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

I recently posted about a basic XML writer in C++ and got a lot of great feedback. Well, i'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

I recently posted about a basic XML writer in C++ and got a lot of great feedback. Well, I'm back with an updated version of the XML writer that is a bit less basic, but hope it's better than the previous. I've implemented a stack, better handling of the string writing and attributes, and default and custom constructors. I have yet to implement better error checking, but I am aware and am looking into it.

Source Link
CodeMonkey
  • 451
  • 1
  • 4
  • 12
Loading
default

AltStyle によって変換されたページ (->オリジナル) /