Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. #ifndef Xml_H
  2. #define Xml_H
  3.  
  4. #include <fstream>
  5. #include <iostream>
  6. #include "LinkStack.h"
  7. #include <string>
  8. #include <vector>
  9. #include <windows> //Error reporting with message boxes.
  10.  
  11. namespace Xml {
  12.  
  13.  
  14. enum class ErrorCode {
  15.  
  16. None,
  17. StreamError,
  18. FileExistError,
  19. ElementCloseError,
  20. ElementTagsNotEmpty
  21.  
  22. };
  23.  
  24. class Error {
  25. public:
  26. /**
  27. Constructor
  28. */
  29. Error();
  30.  
  31. /**
  32. Throws Error Message and displays the message in messagebox
  33. */
  34. void ThrowError(ErrorCode errorCode);
  35.  
  36. private:
  37. ErrorCode err;
  38. const char* errMsg;
  39. };
  40.  
  41. Error::Error() :
  42. err(ErrorCode::None),
  43. errMsg(nullptr)
  44. {
  45.  
  46. }
  47.  
  48. void Error::ThrowError(ErrorCode errorCode) {
  49. err = errorCode;
  50. if (errorCode != ErrorCode::None) {
  51.  
  52. switch (errorCode) {
  53. case ErrorCode::StreamError:
  54. errMsg = "Stream error has occured";
  55. break;
  56. case ErrorCode::FileExistError:
  57. errMsg = "File does not exist";
  58. break;
  59. case ErrorCode::ElementCloseError:
  60. errMsg = "No elements to close";
  61. break;
  62. case ErrorCode::ElementTagsNotEmpty:
  63. errMsg = "Not all element tags are closed. Closing remaining tags";
  64. break;
  65. }
  66. MessageBox(NULL, errMsg, "Error Has Occured", MB_ICONEXCLAMATION | MB_OK);
  67. }
  68. errMsg = nullptr;
  69. }
  70.  
  71. class XmlReader : private Error {
  72.  
  73. public:
  74. /**
  75. Child Node
  76. */
  77. struct cNode {
  78. std::vector<cNode> childNode;
  79. std::string strInnerText;
  80. std::vector<std::string> strAttributeName;
  81. std::vector<std::string> strAttributeValue;
  82. bool hasChildNode();
  83. };
  84.  
  85. /**
  86. Parent Node
  87. */
  88. struct pNode {
  89. std::vector<cNode> childNode;
  90. std::string strInnerText;
  91. std::vector<std::string> strAttributeName;
  92. std::vector<std::string> strAttributeValue;
  93. bool hasChildNode();
  94. };
  95.  
  96. /**
  97. Structure of parent and child nodes
  98. */
  99. struct node {
  100. std::vector<pNode> parentNode;
  101. std::string strInnerText;
  102. std::vector<std::string> strAttributeName;
  103. std::vector<std::string> strAttributeValue;
  104. };
  105.  
  106. /**
  107. Constructor
  108. */
  109. XmlReader();
  110.  
  111. /**
  112. Loads the file
  113. */
  114. bool load(std::string strFileName);
  115.  
  116. /**
  117. Closed the file
  118. */
  119. void close();
  120.  
  121. /*
  122. Gets Node by Tag Name
  123. */
  124. XmlReader::node getElementByTagName(std::string strTagName);
  125.  
  126. private:
  127. /**
  128. Checks if the file exists
  129. */
  130. bool exists(std::string strFileName);
  131.  
  132. /**
  133. Checks if the file is open
  134. */
  135. bool isOpen();
  136.  
  137. /**
  138. Gets child nodes
  139. */
  140. XmlReader::cNode getChildNodes();
  141.  
  142. /**
  143. Gets Attributes
  144. */
  145. void getAttributes(std::string& strOpenTag, std::string& strAttName, std::string& strAttValue);
  146.  
  147. /*
  148. Get Inner Text
  149. */
  150. void getInnerText(std::string& strInLine, std::string& strInnerText);
  151.  
  152. std::ifstream& goToLine(std::ifstream& file, int lineNUm);
  153.  
  154. std::ifstream m_ifsInFile;
  155. std::string m_strParentNode;
  156. std::size_t m_nLineNum;
  157. bool m_bDocOpen;
  158. LinkStack<std::string> m_lsstrChildNodeName;
  159. };
  160.  
  161. XmlReader::XmlReader() {
  162. m_bDocOpen = false;
  163. m_strParentNode = "";
  164. m_nLineNum = 0;
  165. }
  166.  
  167. bool XmlReader::load(std::string strFileName) {
  168. if (!exists(strFileName)) {
  169. Error::ThrowError(ErrorCode::FileExistError);
  170. return false;
  171. }
  172. else {
  173. m_ifsInFile.open(strFileName, std::ios::in);
  174. }
  175. m_bDocOpen = isOpen();
  176. if (!m_bDocOpen) {
  177. Error::ThrowError(ErrorCode::StreamError);
  178. return false;
  179. }
  180. return true;
  181. }
  182.  
  183. void XmlReader::close() {
  184. m_ifsInFile.close();
  185. }
  186.  
  187. bool XmlReader::exists(std::string strFileName) {
  188. std::ifstream ifsCheckFile(strFileName);
  189. return !!ifsCheckFile;
  190. }
  191.  
  192. bool XmlReader::isOpen() {
  193. if (!m_ifsInFile.is_open()) {
  194. return false;
  195. }
  196. return true;
  197. }
  198.  
  199. XmlReader::node XmlReader::getElementByTagName(std::string strTagName) {
  200. XmlReader::node node;
  201. XmlReader::pNode parentNode;
  202. XmlReader::cNode childNode;
  203. std::string strInnerText;
  204. std::string strAttributeName;
  205. std::string strAttributeValue;
  206. std::size_t sz = strTagName.size();
  207. std::string strInLine;
  208. std::string strTmp;
  209. std::string strOpenTag;
  210. int posLt = 0;
  211. int posGt = 0;
  212. int posFAtr = 0;
  213. int posLAtr = 0;
  214. int posLastAtr = 0;
  215. m_strParentNode = strTagName;
  216. while (std::getline(m_ifsInFile, strInLine, 'n')) {
  217. parentNode.strAttributeName._Pop_back_n(parentNode.strAttributeName.size());
  218. parentNode.strAttributeValue._Pop_back_n(parentNode.strAttributeValue.size());
  219. m_nLineNum = m_ifsInFile.tellg();
  220. if (strInLine != "") {
  221.  
  222. if (strInLine[0] == 't') {
  223. std::vector<int> tabPos;
  224. for (int i = 0; i < strInLine.size(); i++) {
  225. if (strInLine[i] == 't') {
  226. tabPos.push_back(i);
  227. }
  228. }
  229. for (int i = tabPos.size(); i > 0; i--) {
  230. strInLine.erase(tabPos[i - 1], 1);
  231. tabPos.pop_back();
  232. }
  233. }
  234.  
  235. if (strInLine.substr(1, sz) == strTagName) {
  236.  
  237. posLt = strInLine.find_last_of("<");
  238. posGt = strInLine.find_last_of(">");
  239.  
  240. strTmp = strInLine.substr(posLt + 1, posGt - posLt - 1);
  241.  
  242. if (strTmp == "/" + strTagName) {
  243.  
  244. posLt = strInLine.find_first_of("<");
  245. posGt = strInLine.find_first_of(">");
  246.  
  247. strOpenTag = strInLine.substr(posLt + 1, posGt - posLt - 1);
  248.  
  249. if (strOpenTag.size() > sz) {
  250.  
  251. while (strOpenTag.size() > sz) {
  252.  
  253. getAttributes(strOpenTag, strAttributeName, strAttributeValue);
  254. parentNode.strAttributeName.push_back(strAttributeName);
  255. parentNode.strAttributeValue.push_back(strAttributeValue);
  256.  
  257. }
  258. }
  259. getInnerText(strInLine, strInnerText);
  260. parentNode.strInnerText = strInnerText;
  261.  
  262. }
  263. else {
  264. childNode = getChildNodes();
  265. parentNode.childNode.push_back(childNode);
  266. }
  267.  
  268. node.parentNode.push_back(parentNode);
  269.  
  270. }
  271.  
  272. }
  273.  
  274. }
  275. return node;
  276. }
  277.  
  278.  
  279. XmlReader::cNode XmlReader::getChildNodes() {
  280. XmlReader::cNode tmpNode;
  281. XmlReader::cNode childNode;
  282. std::string strInLine;
  283. std::string strInnerText;
  284. std::string strAttributeName;
  285. std::string strAttributeValue;
  286. std::string strTagName;
  287. std::string strEndTag;
  288. std::string strOpenTag;
  289. std::size_t sz;
  290. int posF = 0;
  291. int posL = 0;
  292. int posS = 0;
  293. int posLt = 0;
  294. int posGt = 0;
  295. bool endTag = false;
  296.  
  297. if (m_nLineNum != m_ifsInFile.tellg()) {
  298. goToLine(m_ifsInFile, m_nLineNum);
  299. }
  300.  
  301. while (!endTag) {
  302.  
  303. std::getline(m_ifsInFile, strInLine, 'n');
  304. m_nLineNum = m_ifsInFile.tellg();
  305.  
  306. if (strInLine != "") {
  307. posF = strInLine.find_first_of("<");
  308. posL = strInLine.find_first_of(">");
  309. posS = strInLine.find_first_of(" ");
  310.  
  311. if (posS < posL && posS > posF) {
  312. strTagName = strInLine.substr(posF + 1, posS - posF - 1);
  313. }
  314. else {
  315. strTagName = strInLine.substr(posF + 1, posL - posF - 1);
  316. }
  317.  
  318. if (strTagName.substr(0, 1) == "/") {
  319. strTagName.erase(0, 1);
  320. }
  321.  
  322. m_lsstrChildNodeName.push(strTagName);
  323. posF = strInLine.find_last_of("<");
  324. posL = strInLine.find_last_of(">");
  325. if (strInLine.substr(posF + 1, posL - posF - 1) != "/" + m_lsstrChildNodeName.top()) {
  326.  
  327. childNode.strAttributeName._Pop_back_n(childNode.strAttributeName.size());
  328. childNode.strAttributeValue._Pop_back_n(childNode.strAttributeValue.size());
  329.  
  330. sz = strTagName.size();
  331.  
  332. if (strInLine.substr(1, sz) == strTagName) {
  333.  
  334. posLt = strInLine.find_first_of("<");
  335. posGt = strInLine.find_first_of(">");
  336.  
  337. strOpenTag = strInLine.substr(posLt + 1, posGt - posLt - 1);
  338.  
  339. if (strOpenTag.size() > sz) {
  340.  
  341. while (strOpenTag.size() > sz) {
  342.  
  343. getAttributes(strOpenTag, strAttributeName, strAttributeValue);
  344. childNode.strAttributeName.push_back(strAttributeName);
  345. childNode.strAttributeValue.push_back(strAttributeValue);
  346.  
  347. }
  348.  
  349. }
  350.  
  351. posLt = strInLine.find_last_of("<");
  352. posGt = strInLine.find_last_of(">");
  353.  
  354. strEndTag = strInLine.substr(posLt + 1, posGt - posLt - 1);
  355.  
  356. if (strEndTag == "/" + strTagName) {
  357.  
  358. getInnerText(strInLine, strInnerText);
  359. childNode.strInnerText = strInnerText;
  360.  
  361. } else {
  362.  
  363. tmpNode = getChildNodes();
  364. childNode.childNode.push_back(tmpNode);
  365.  
  366. }
  367.  
  368. } else {
  369. endTag = true;
  370. }
  371. } else {
  372. getInnerText(strInLine, strInnerText);
  373. childNode.strInnerText = strInnerText;
  374. m_lsstrChildNodeName.pop();
  375. endTag = true;
  376. }
  377. }
  378. }
  379.  
  380. return childNode;
  381.  
  382. }
  383.  
  384. void XmlReader::getAttributes(std::string& strOpenTag, std::string& strAttName, std::string& strAttValue) {
  385. int posF = 0;
  386. int posL = 0;
  387.  
  388. posF = strOpenTag.find_first_of(" ");
  389. posL = strOpenTag.find_first_of("=");
  390.  
  391. strAttName = strOpenTag.substr(posF + 1, posL - posF - 1);
  392. strOpenTag.erase(posF, posL - posF + 1);
  393.  
  394. posF = strOpenTag.find_first_of(""");
  395. posL = posF;
  396. posF = strOpenTag.find(""", posF + 1);
  397. strAttValue = strOpenTag.substr(posL + 1, posF - posL - 1);
  398. strOpenTag.erase(posL, posF - posL + 1);
  399. }
  400.  
  401.  
  402. void XmlReader::getInnerText(std::string& strInLine, std::string& strInnerText) {
  403. int posLt = 0;
  404. int posGt = 0;
  405.  
  406. posLt = strInLine.find_last_of("<");
  407. posGt = strInLine.find_first_of(">");
  408.  
  409. strInnerText = strInLine.substr(posGt + 1, posLt - posGt - 1);
  410. }
  411.  
  412.  
  413. std::ifstream& XmlReader::goToLine(std::ifstream& file, int lineNum) {
  414. file.seekg(std::ios::beg);
  415. file.seekg(lineNum);
  416.  
  417. return file;
  418. }
  419.  
  420.  
  421. bool XmlReader::pNode::hasChildNode() {
  422. if (XmlReader::pNode::childNode.size() != 0) {
  423. return true;
  424. }
  425. return false;
  426. }
  427.  
  428.  
  429. bool XmlReader::cNode::hasChildNode() {
  430. if (XmlReader::cNode::childNode.size() != 0) {
  431. return true;
  432. }
  433. return false;
  434. }
  435.  
  436. }
  437.  
  438. #endif
  439.  
  440. #include "Xml.h"
  441.  
  442. int main() {
  443.  
  444. Xml::XmlReader xmlR;
  445.  
  446. if (xmlR.load("path\to\xml\doc")) {
  447.  
  448. Xml::XmlReader::node xmlNode;
  449.  
  450. xmlNode = xmlR.getElementByTagName("TagName");
  451.  
  452. std::cout << xmlNode.parentNode[0].//enter rest of statement to get child nodes,
  453. //attributes, or what ever that is collected.
  454.  
  455. }
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement