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;
}

转载于:https://www.cnblogs.com/janko208/archive/2012/06/14/2548911.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值