本段代码通过ACEXML来解析一个XML文件,并且生成了一棵树,树的存储采用孩子兄弟存储的方法。
在连接生成可执行文件的时候,连接一下文件:
ACEd.lib
ACEXMLd.lib
ACEXML_Parserd.lib
iphlpapi.lib
CTree
#pragma once
#include "string"
using namespace std;
class CTree
{
public:
typedef enum{LEAF = 0x00, NOTLEAF = 0x01} ATTRIBUTE;
public:
CTree(void);
CTree(ATTRIBUTE attribute, string value);
~CTree(void);
private:
ATTRIBUTE m_attribute;
string m_value;
CTree* m_left;
CTree* m_right;
public:
void addnode(CTree* children);
void output(int indent);
ATTRIBUTE getattribute();
string getvalue();
CTree* getleft();
CTree* getright();
int getchildnumber();
CTree* getfirstchild();
CTree* getnextchild();
CTree* getchild(int index);
void destroy();
};
#include "Tree.h"
CTree::CTree(void)
{
m_attribute = NOTLEAF;
m_value = "";
m_left = NULL;
m_right = NULL;
}
CTree::CTree(ATTRIBUTE attribute, string value)
{
m_attribute = attribute;
m_value = value;
m_left = NULL;
m_right = NULL;
}
CTree::~CTree(void)
{
printf("destroy value=%s\n", m_value.c_str());
}
CTree::ATTRIBUTE CTree::getattribute()
{
return m_attribute;
}
CTree* CTree::getleft()
{
return m_left;
}
CTree* CTree::getright()
{
return m_right;
}
string CTree::getvalue()
{
return m_value;
}
void CTree::destroy()
{
if (m_left != NULL)
{
m_left->destroy();
}
if (m_right != NULL)
{
m_right->destroy();
}
delete this;
}
void CTree::addnode(CTree* child)
{
if (m_left == NULL)
{
m_left = child;
}
else
{
CTree* ptemp = m_left;
while(ptemp->m_right != NULL)
{
ptemp = ptemp->m_right;
}
ptemp->m_right = child;
}
}
void CTree::output(int indent)
{
//printf("value=%s attri=%c this=%08X left=%08X right=%08X\n", m_value.c_str(), ((m_attribute == LEAF) ? 'C' : 'P'), this, m_left, m_right);
//printf("%s(deep=%d)\n", m_value.c_str(), indent);
printf("%s\n", m_value.c_str());
if (m_left != NULL)
{
indent++;
for(int i=0; i<indent; i++)
{
printf("|____");
}
m_left->output(indent);
indent--;
}
if (m_right != NULL)
{
for(int i = 0; i < indent; i++)
{
printf("|____");
}
m_right->output(indent);
}
}
int CTree::getchildnumber()
{
int number = 0;
if (m_left == NULL)
{
return number;
}
else
{
number++;
CTree* temp = m_left;
while(temp->m_right != NULL)
{
number++;
temp = temp->m_right;
}
}
return number;
}
CTree* CTree::getfirstchild()
{
return m_lef