Snippets
Created by
John Martini
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include "stdafx.h"
#include <iostream>
#include "tinyxml2.h"
#include <vector>
using namespace tinyxml2;
using namespace std;
class Attribute
{
public:
std::string Key;
std::string Value;
};
class AttributeBlock
{
public:
std::string Name;
vector<Attribute> Attributes;
};
class Node
{
public:
std::string ClassName;
std::string Enabled;
std::string ID;
vector<AttributeBlock> AttributeBlocks;
};
void LoadGraph(const char* xmlFilepath)
{
// Used to store all the nodes
vector<Node> Nodes;
XMLDocument doc;
XMLError eResult = doc.LoadFile(xmlFilepath);
if (eResult != XML_SUCCESS)
{
printf("Failed to load file: \"%s\"\n", xmlFilepath);
return;
}
printf("Successfully loaded file: %s:\n", xmlFilepath);
// Parse file
XMLElement *xRoot = doc.FirstChildElement("graph");
if (xRoot)
{
XMLElement * xNodes = xRoot->FirstChildElement("nodes");
if (xNodes)
{
XMLElement * xNode = xNodes->FirstChildElement("node");
if (xNode)
{
// create Node
Node node;
while (xNode)
{
// get node properties
node.ID = xNode->Attribute("id");
node.ClassName = xNode->Attribute("className");
node.Enabled = xNode->Attribute("enabled");
printf("Node:\t%s\n", xNode->Attribute("id"));
printf("\t%s\n", xNode->Attribute("className"));
printf("\t%s\n", xNode->Attribute("virtualName"));
printf("\t%s\n", xNode->Attribute("enabled"));
// get node attribute blocks
XMLElement * xAttributeBlocks = xNode->FirstChildElement("attributeblocks");
if (xAttributeBlocks)
{
XMLElement * xAttributeBlock = xAttributeBlocks->FirstChildElement("attributeblock");
if (xAttributeBlock)
{
// get attribute block properties
AttributeBlock attributeBlock;
attributeBlock.Name = xAttributeBlock->Attribute("name");
printf("\t%s\n", xAttributeBlock->Attribute("name"));
XMLElement * xAttribute = xAttributeBlock->FirstChildElement("attribute");
if (xAttribute)
{
// create attribute
Attribute attr;
while (xAttribute)
{
// get attribute properties
attr.Key = xAttribute->Attribute("key");
attr.Value = xAttribute->Attribute("value");
printf("\t\t%s\n", xAttribute->Attribute("key"));
printf("\t\t%s\n", xAttribute->Attribute("value"));
// append attribute to attribute block
attributeBlock.Attributes.push_back(attr);
// recursive loop
xAttribute = xAttribute->NextSiblingElement("attribute");
}
}
// append attribute block to node
node.AttributeBlocks.push_back(attributeBlock);
}
}
// append node to dynamic array
Nodes.push_back(node);
// recursive loop
xNode = xNode->NextSiblingElement("node");
}
}
}
cout << endl;
}
cout << "Node Count: " << Nodes.size() << '\n';
cout << "End Parse\n";
}
int main()
{
LoadGraph("C:/Users/Martini/Desktop/trash/graphTestings/testGraph.nx");
return 0;
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.