bcb treeview与xml互相导入导出

用的是bcb的treeview、tinyxml读取和存放xml数据

#include "tinyxml.h"
#include "tinystr.h"
#include <vector.h>

typedef struct tagXmlNodeAttr
{
    AnsiString strAttrName;
    AnsiString strAttrValue;
}xmlNodeAttr;

typedef struct tagXmlNodeData
{
    AnsiString strNodeName;
    vector<tagXmlNodeAttr *> vNodeAttr;
    AnsiString strNodeValue;
}xmlNodeData;

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TTreeView *TreeView1;
private:	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
    __fastcall ~TForm1();
    void __fastcall TreeToXML(TTreeNode *node,TiXmlElement *parentNode,TiXmlDocument *xmlDocument);
    void __fastcall XMLtoTree(TTreeNode *treeNode,TiXmlNode *parentNode,TTreeView *treeView);
    bool __fastcall LoadConfFile(char * path,TTreeView *treeView); //
    bool __fastcall SaveConfFile(char * path,char * verison,char * encoding,char * standalone);
    void __fastcall ClearTreeView(TTreeView *treeView);
private:
    TiXmlDocument *m_xmlDocument;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;

  

void __fastcall TForm1::ClearTreeView(TTreeView *treeView)
{
    for(int i = 0;i < treeView->Items->Count;i++)
    {
        TTreeNode *treeNode = treeView->Items->Item[i];
        tagXmlNodeData *pXmlNodeData = (tagXmlNodeData *)treeNode->Data;
        vector<tagXmlNodeAttr *> vNodeAttr = pXmlNodeData->vNodeAttr;
        for(vector<tagXmlNodeAttr *>::iterator iter = vNodeAttr.begin();iter != vNodeAttr.end();++iter)
        {
            tagXmlNodeAttr *pXmlNodeAttr = *iter;
            if(pXmlNodeAttr)
            {
                delete pXmlNodeAttr;
            }
        }
        delete pXmlNodeData;
    }
    treeView->Items->Clear();
}

void __fastcall TForm1::TreeToXML(TTreeNode *treeNode,TiXmlElement *parentNode,TiXmlDocument *xmlDocument)
{
    TiXmlElement *pChildElement;
    if(treeNode == NULL)
    {
        return;
    }

    while(treeNode)
    {
        tagXmlNodeData *pXmlNodeData = (tagXmlNodeData *)treeNode->Data;
        vector<tagXmlNodeAttr *> vNodeAttr = pXmlNodeData->vNodeAttr;
        if(parentNode == NULL)
        {
            pChildElement = new TiXmlElement(pXmlNodeData->strNodeName.c_str());
            xmlDocument->LinkEndChild(pChildElement);
            parentNode = pChildElement;
        }
        else
        {
            pChildElement = new TiXmlElement(pXmlNodeData->strNodeName.c_str());
            parentNode->LinkEndChild(pChildElement);
        }

        for(vector<tagXmlNodeAttr *>::iterator iter = vNodeAttr.begin();iter != vNodeAttr.end();++iter)
        {
            tagXmlNodeAttr *pXmlNodeAttr = *iter;
            pChildElement->SetAttribute(pXmlNodeAttr->strAttrName.c_str(),pXmlNodeAttr->strAttrValue.c_str());
        }

        AnsiString strNodeValue = pXmlNodeData->strNodeValue;
        if(!strNodeValue.IsEmpty())
        {
            TiXmlText *pXmlText = new TiXmlText(strNodeValue.c_str());
            pChildElement->LinkEndChild(pXmlText);
        }

        TreeToXML(treeNode->getFirstChild(),pChildElement,NULL);
        treeNode = treeNode->getNextSibling();
    }
}

void __fastcall TForm1::XMLtoTree(TTreeNode *treeNode,TiXmlNode *parentNode,TTreeView *treeView)
{
    if(parentNode == NULL)
    {
        return;
    }

    TiXmlNode* pChildNode = parentNode;
    while(pChildNode)
    {
        int nType = pChildNode->Type();
        if(nType == TiXmlNode::TINYXML_ELEMENT)
        {
            TTreeNode *tNode;
            tagXmlNodeData *pXmlNodeData = new tagXmlNodeData;

            tNode = treeView->Items->AddChild(treeNode,pChildNode->Value());
            tNode->Data = pXmlNodeData;

            pXmlNodeData->strNodeName = pChildNode->Value();
            TiXmlAttribute* pXmlAttr = pChildNode->ToElement()->FirstAttribute();
            if(pXmlAttr)
            {
                TiXmlNode* pTempNode = pChildNode;
                while(pTempNode)
                {
                    while(pXmlAttr)
                    {
                        tagXmlNodeAttr *pXmlNodeAttr = new tagXmlNodeAttr;
                        pXmlNodeAttr->strAttrName = pXmlAttr->Name();
                        pXmlNodeAttr->strAttrValue = pXmlAttr->Value();
                        pXmlNodeData->vNodeAttr.push_back(pXmlNodeAttr);
                        pXmlAttr = pXmlAttr->Next();
                    }
                    pTempNode = pTempNode->NextSiblingElement();
                }
            }
            XMLtoTree(tNode,pChildNode->FirstChild(),treeView);
        }
        else if(nType == TiXmlNode::TINYXML_TEXT)
        {
            tagXmlNodeData *pXmlNodeData = (tagXmlNodeData *)treeNode->Data;
            pXmlNodeData->strNodeValue = pChildNode->Value();
        }
        pChildNode = pChildNode->NextSibling();
    }
}

bool __fastcall TForm1::LoadConfFile(char *path,TTreeView *treeView)
{
    ClearTreeView(treeView);
    TiXmlDocument *xmlDocument = new TiXmlDocument(path);
    xmlDocument->LoadFile();
    TiXmlElement *RootElement = xmlDocument->RootElement();
    XMLtoTree(NULL,RootElement,treeView);
    delete xmlDocument;
}

bool __fastcall TForm1::SaveConfFile(char * path,char * verison,char * encoding,char * standalone)
{
    TiXmlDocument *xmlDocument = new TiXmlDocument();
    TiXmlDeclaration *pDecl = new TiXmlDeclaration(verison,encoding,standalone);
    xmlDocument->LinkEndChild(pDecl);
    TreeToXML(TreeView1->Items->GetFirstNode(),NULL,xmlDocument);
    bool bSuccess = xmlDocument->SaveFile(path);
    delete xmlDocument;
    return bSuccess;
}

你可能感兴趣的:(bcb treeview与xml互相导入导出)