c++ tinyxml

做了一个中控项目,对xml的读取不怎么记得了,先记录在这里

bool ReadXML(char *path)
{
    //////////////////////////////////////////////////////////////////////////  read xml file    
    TiXmlDocument * myDocument = new TiXmlDocument(path);  //加载文件数据  
    bool IsLoad=myDocument->LoadFile();  //获取根节点  
    if(!IsLoad)
    {
    
        cout<<"Could not load:"<<path<<endl;
        return false;
    }
    else
    {
        cout<<"Load Succssed:"<<path<<endl;
    }
    
    TiXmlElement* rootElement = myDocument->RootElement();  //Root元素       count has how many DV
    
    TiXmlElement *xjf,*xjiqi,*xnode;
    xjf = rootElement->FirstChildElement();  // FirstChild元素 
    numJF =0;
    for(;xjf != NULL; xjf = xjf ->NextSiblingElement())
    {
        numJF++;
        
    }
    cout<<"numJF:"<<numJF<<endl;
    mJF = new JIFANG[numJF];
    int countjf=0,countjq =0 ,numNode = 0;
    xjf = rootElement ->FirstChildElement();
    for(;xjf!= NULL;xjf = xjf->NextSiblingElement())
    {
        TiXmlAttribute* attributeOfJF = xjf->FirstAttribute();  
        mJF[countjf].jfname =attributeOfJF->Value();
        
        countjq =0;
        xjiqi = xjf->FirstChildElement();
        for(;xjiqi !=NULL;xjiqi =xjiqi->NextSiblingElement())
        {
            countjq++;
        }
        cout<<"numjiqi:"<<countjq<<endl;

        
        mJF[countjf].init(countjq);
        mJF[countjf].mrect.x = paiban[countjf][0];    //set jf x,y ,w,h
        mJF[countjf].mrect.y = paiban[countjf][1];
        countjq =0;
        xjiqi = xjf->FirstChildElement();
        for(;xjiqi !=NULL;xjiqi =xjiqi->NextSiblingElement())
        {
            xnode = xjiqi->FirstChildElement();
            numNode =0 ;
            mJF[countjf].pSock[countjq].mrect.x =paiban[countjq][0];    //set jq x y w h
            mJF[countjf].pSock[countjq].mrect.y =paiban[countjq][1];
            for(;xnode!= NULL; xnode =xnode->NextSiblingElement())
            {
                if(numNode == 0)
                {
                    
                    mJF[countjf].pSock[countjq].name =xnode->GetText();
                    //cout<<"name:"<<mJF[countjf].pSock[countjq].name.c_str();
                }
                if(numNode == 1)
                {
                    
                    mJF[countjf].pSock[countjq].m_ip= xnode->GetText();
                    //cout<<"   ip:"<<mJF[countjf].pSock[countjq].name.c_str()<<endl;
                
                }
                numNode++;
            }
            countjq++;
        }
        countjf++;
    }
    

    for(int numofjf =0 ;numofjf<numJF ; numofjf ++)
    {
        printf("%15s   数量:%d \n",mJF[numofjf].jfname.c_str(),mJF[numofjf].nDV);
        for(int numofjq =0; numofjq<mJF[numofjf].nDV;numofjq++ )
        {
            printf("%20s ip:%s \n",mJF[numofjf].pSock[numofjq].name.c_str(),mJF[numofjf].pSock[numofjq].m_ip.c_str());
        
        }
        cout<<endl;    
    
    }
    return true;
//////////////////////////////////////////////////////////////////////     laster do something

}

 

posted on 2013-01-15 11:52  c_dragon  阅读(349)  评论(0编辑  收藏  举报

导航