VC 使用tinyxml库解析xml文件,节点切换,元素取值

////////////////////////////////////////////////

c.xml 如下

/////////////////////////////////



  ЫЮЬх
 
  20080202190047182.jpg
 
 
          this1
          this2

////////////////////////////////////////////////

b.xml 如下

/////////////////////////////////



    pxN/s84Vb5kxMzQzNjM4
    OX7q6n8PPO0xNg==
    Q8VULnOFagk=
    MA==
    0
    http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png


    123
    OX7q6n8PPO0xNg==6
    Q8VULnOFagk=6
    MA==6
    
          comehere
          love有
    

    06
    http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png6

/////////////////////////////////////////////////////////////

#include
#include
#include
#include
#include
#include
#include "tinyxml.h"
using namespace std;
 void getdatafromxml();
  void ReadData();
 const char* kFileName = "b.xml";

int main() {
 // WriteData();
  //ReadData();
  getdatafromxml();
  getchar();
  return 0;
}
void getdatafromxml()
{
   
    char xmlFile[256] = "c.xml";
    TiXmlDocument doc(xmlFile);
    bool loadOkay = doc.LoadFile();
    if (!loadOkay)
    {
       cout<<"Load failed!"<     }
    setlocale( LC_CTYPE, "chs" );
 
    TiXmlNode* node = 0;
    TiXmlNode*nextnode=0;
    TiXmlElement* todoElement = 0;
    node = doc.FirstChild( "system" );
    if (0 == node)
        return;
    if (nextnode=node->FirstChild("fontname"))
    {
         todoElement=nextnode->ToElement();
         string strGet = todoElement->GetText();
         cout<     }
    if (nextnode=node->FirstChild("tinyfontsize"))
    {
        todoElement=nextnode->ToElement();
        int x;
        todoElement->QueryIntAttribute("size",&x);    //有属性的元素,使用元素类的对象函数,取得属性值
        cout<     }
    if (nextnode = node->FirstChild("backfile"))
    {
        todoElement = nextnode->ToElement();  //节点对象转换成元素对象,然后通过元素对象调用函数,获取元素值
       string str = todoElement->GetText();
        cout<         /*    sysEntry.background=new ImageWrap;
            if(false==sysEntry.background->loadFile(strChange.doChange(todoElement->GetText()).c_str()))
            {
                delete sysEntry.background;
                sysEntry.background=0;
            }*/
    }
    if (nextnode = node->FirstChild("window"))
    {
        todoElement = nextnode->ToElement();
        //    sysEntry.minWindowSize.cx=640;
        //sysEntry.minWindowSize.cy=480;
        int value;
        todoElement->QueryIntAttribute("minWidth",&value);
        cout<         todoElement->QueryIntAttribute("minHeight",&value);
        cout<     }
    if (nextnode=node->FirstChild("adjuststatic"))
    {
        TiXmlNode* childNode = 0;
      
        if (childNode = nextnode->FirstChild("test1"))  //向下寻找节点
        {
        //    childNode=nextnode->FirstChild("test1");
            todoElement = childNode->ToElement();
            string str = todoElement->GetText();
            cout<             childNode = nextnode->FirstChild("test2");
            todoElement  = childNode->ToElement();
            str = todoElement->GetText();
            cout<         }
    }
 
}
 void ReadData() {
  TiXmlDocument xdoc;
  if (!xdoc.LoadFile(kFileName)) {
    return;
  }
 // xdoc.Print();
  TiXmlElement* filexml = xdoc.RootElement();
  if (filexml == NULL) {
    return;
  }
  while (filexml != NULL) {
    TiXmlElement* xname = filexml->FirstChildElement("name");
    std::string name =    xname->GetText();
    cout<     TiXmlElement* uniqueid = filexml->FirstChildElement("uniqueid");
    std::string uni = uniqueid->GetText();
    cout<     TiXmlElement* imstatus = filexml->FirstChildElement("imstatus");
     std::string imst = imstatus->GetText();
    cout<

    TiXmlElement* loginType = filexml->FirstChildElement("loginType");
     std::string login = loginType->GetText();
    cout<

    TiXmlNode* childnode = 0;
    TiXmlNode* nextnode  = 0;

    //if(nextnode  = childnode->FirstChild("test"))
    //{
    //        nextnode = childnode->FirstChild("test1");
    //        filexml = nextnode->ToElement();
    //        string str = filexml->GetText();
 //           cout<

    //        nextnode = childnode->FirstChild("test2");
    //        filexml = nextnode->ToElement();
    //        str = filexml->GetText();
 //           cout<     //}
    TiXmlElement* logoindex = filexml->FirstChildElement("logoindex");
     std::string loginde = logoindex->GetText();
    cout<     TiXmlElement* logourl = filexml->FirstChildElement("logourl");
     std::string loginu = logourl->GetText();
    cout<

   filexml = filexml->NextSiblingElement();
  }
}

你可能感兴趣的:(C++)