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

本文展示了如何在VC环境下利用TinyXML库解析XML文件,详细讲解了读取XML文件中不同节点和元素值的方法,包括字体名、字体大小、背景文件、窗口尺寸等,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c.xml 如下

/

<?xml version="1.0" encoding="x-cp20936"?>
<system>
  <fontname>ЫЮЬх</fontname>
  <tinyfontsize size="12"/>
  <backfile>20080202190047182.jpg</backfile>
  <window minWidth="640" minHeight="480"/>
  <adjuststatic>
        <test1>  this1 </test1>
        <test2>  this2 </test2>
</adjuststatic>
</system>

b.xml 如下

/

<?xml version="1.0"?>
<account>
    <name>pxN/s84Vb5kxMzQzNjM4</name>
    <uniqueid>OX7q6n8PPO0xNg==</uniqueid>
    <imstatus>Q8VULnOFagk=</imstatus>
    <loginType>MA==</loginType>
    <logoindex>0</logoindex>
    <logourl>http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png</logourl>
</account>
<account>
    <name>123</name>
    <uniqueid>OX7q6n8PPO0xNg==6</uniqueid>
    <imstatus>Q8VULnOFagk=6</imstatus>
    <loginType>MA==6</loginType>
    <test>
        <test1>  comehere </test1>
        <test2>  love有 </test2>
    </test>
    <logoindex>06</logoindex>
    <logourl>http://downhdlogo.yy.com/hdlogo/100100/100/100/32/1849328616/u18493286165K0gQUM.png6</logourl>
</account>

/

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <cassert>
#include <Windows.h>
#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!"<<endl;
    }
    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<<strGet<<endl;
    }
    if (nextnode=node->FirstChild("tinyfontsize"))
    {
        todoElement=nextnode->ToElement();
        int x;
        todoElement->QueryIntAttribute("size",&x);    //有属性的元素,使用元素类的对象函数,取得属性值
        cout<<x<<endl;
    }
    if (nextnode = node->FirstChild("backfile"))
    {
        todoElement = nextnode->ToElement();  //节点对象转换成元素对象,然后通过元素对象调用函数,获取元素值
       string str = todoElement->GetText();
        cout<<str<<endl;
        /*    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<<value<<endl;
        todoElement->QueryIntAttribute("minHeight",&value);
        cout<<value<<endl;
    }
    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<<str<<endl;
            childNode = nextnode->FirstChild("test2");
            todoElement  = childNode->ToElement();
            str = todoElement->GetText();
            cout<<str<<endl;
        }
    }
 
}
 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<<name<<endl;
    TiXmlElement* uniqueid = filexml->FirstChildElement("uniqueid");
    std::string uni = uniqueid->GetText();
    cout<<uni<<endl;
    TiXmlElement* imstatus = filexml->FirstChildElement("imstatus");
     std::string imst = imstatus->GetText();
    cout<<imst<<endl;

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

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

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

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

   filexml = filexml->NextSiblingElement();
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值