Snippets

John Martini Parsing nexus XML file to usable nodes using TinyXML2.

Created by John Martini last modified
#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;
}
<?xml version="1.0" encoding="UTF-8"?>
<graph uuid="12345239458-23985-239">
    <containers/>
    <nodes>
        <node backgroundColor="RGBA(60,120,215,200)" enabled="True" className="Objects By Name" image=":/images/icon.ico" virtualName="Objects By Name" isContainer="False" id="1" zValue="3">
            <viewblocks>
                <rect x="-113" y="-35" width="150" height="65"/>
            </viewblocks>
            <outputs>
                <item name="Objects" type="Objects" limit="0"/>
            </outputs>
            <attributeblocks>
                <attributeblock name="Properties">
                    <attribute key="nodenames" label="Node Names" value="Teapot*,Sphere001,Box*" type="textbox"/>
                </attributeblock>
            </attributeblocks>
        </node>
        <node backgroundColor="RGBA(60,120,215,200)" enabled="True" className="Objects By Layer Name" image=":/images/icon.ico" virtualName="Objects By Layer Name" isContainer="False" id="2" zValue="2">
            <viewblocks>
                <rect x="-113" y="-128" width="150" height="65"/>
            </viewblocks>
            <outputs>
                <item name="Objects" type="Objects" limit="0"/>
            </outputs>
            <attributeblocks>
                <attributeblock name="Properties">
                    <attribute key="layernames" label="Layer Names" value="Default," type="textbox"/>
                </attributeblock>
            </attributeblocks>
        </node>
    </nodes>
    <connections/>
</graph>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.