RootElement FirstChildElement FirstChildElement NextSiblingElement FirstAttribute

该篇博客介绍了一个XML解析示例,通过TinyXML库读取并打印出"计算机软件班"的学生信息,包括姓名、学号、性别、年龄、电话和地址。程序详细展示了如何获取根元素、子元素及属性,并遍历学生节点。

<Class name="计算机软件班">
    <Students>
        <student name="张三" studentNo="13031001" sex="男" age="22">
            <phone>88208888</phone>
            <address>西安市太白南路二号</address>
        </student>
        <student name="李四" studentNo="13031002" sex="男" age="20">
            <phone>88206666</phone>
            <address>西安市光华路</address>
        </student>
    </Students>
</Class>

 

    程序代码XmlParseExample.cpp如下所示:

#include <iostream>
#include <string>
#include <tinyxml.h>

  using std::string;

  int main()
{
  TiXmlDocument* myDocument = new TiXmlDocument();
  myDocument->LoadFile("Students.xml");
  TiXmlElement* rootElement = myDocument->RootElement();  //Class
  TiXmlElement* studentsElement = rootElement->FirstChildElement();  //Students
  TiXmlElement* studentElement = studentsElement->FirstChildElement();  //Students
  while ( studentElement ) {
    TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();  //获得student的name属性
    while ( attributeOfStudent ) {
      std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;
      attributeOfStudent = attributeOfStudent->Next();
    }
    TiXmlElement* phoneElement = studentElement->FirstChildElement();//获得student的phone元素
    std::cout << "phone" << " : " << phoneElement->GetText() << std::endl;
    TiXmlElement* addressElement = phoneElement->NextSiblingElement();
    std::cout << "address" << " : " << phoneElement->GetText() << std::endl;
    studentElement = studentElement->NextSiblingElement();
  }
  return 0;
}

    程序运行结果如下:
name : 张三
studentNo : 13031001
sex : 男
age : 22
phone : 88208888
address : 88208888
name : 李四
studentNo : 13031002
sex : 男
age : 20
phone : 88206666
address : 88206666 






如下是一个XML片段:

    <Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
        <Person ID="2">
#include "XmlParser.h" 2 #include "InfoReader.h" 3 4 using namespace tinyxml2; 5 6 void GameList::insert(const std::string &game) 7 { 8 mGameSet.insert(game); 9 } 10 11 bool GameList::contains(const std::string &game) 12 { 13 auto iter = mQueryCache.find(game); 14 if (iter != mQueryCache.end()) { 15 return iter->second; 16 } 17 bool ret = mGameSet.count(game) > 0; 18 mQueryCache.push_back(game, ret); 19 return ret; 20 } 21 22 void GameList::clear() 23 { 24 mGameSet.clear(); 25 mQueryCache.clear(); 26 } 27 28 GameListRecorder::GameListRecorder() 29 { 30 } 31 32 GameListRecorder::~GameListRecorder() 33 { 34 } 35 36 void GameListRecorder::loadData(const std::string &path) 37 { 38 XMLDocument doc; 39 if (doc.LoadFile(path.c_str()) != 0) { 40 return; 41 } 42 XMLElement *root = doc.RootElement(); 43 if (root == nullptr) { 44 return; 45 } 46 mGameList.clear(); 47 XMLElement *head = root->FirstChildElement("item"); 48 while (head != 0) { 49 const char* package = head->Attribute("package"); 50 if (package) { 51 mGameList.insert(std::string(package)); 52 } 53 head = head->NextSiblingElement(); 54 } 55 } 56 57 bool GameListRecorder::contains(const std::string &package) 58 { 59 return mGameList.contains(package); 60 } 61 62 ThermalControlRecoder::ThermalControlRecoder() 63 { 64 } 65 66 ThermalControlRecoder::~ThermalControlRecoder() 67 { 68 } 69 70 void ThermalControlRecoder::loadData(const std::string &path) 71 { 72 XMLDocument doc; 73 if (doc.LoadFile(path.c_str()) != 0) { 74 return; 75 } 76 XMLElement *root = doc.RootElement(); 77 if (root == nullptr) { 78 return; 79 } 80 mHeavyModel.clear(); 81 mDefaultModel.clear(); 82 83 XMLElement *heavyList = root->FirstChildElement("heavy_policy"); 84 if (heavyList != nullptr) { 85 XMLElement *heavyHead = heavyList->FirstChildElement("game_control"); 86 while (heavyHead != 0) { 87 ThermalConfig config; 88 config.fps = heavyHead->IntAttribute("fps", 0); 89 for (auto [coreIndex, cluster] : mIndexToCluster) { 90 std::string clusterStr = "cluster" + std::to_string(coreIndex); 91 int level = heavyHead->IntAttribute(clusterStr.c_str(), -1); 92 if (level >= 0) { 93 config.cpuLevel[cluster] = level; 94 } 95 } 96 int tempGear = heavyHead->IntAttribute("temp", ABSOLUTE_ZERO_TEN); 97 if (tempGear != ABSOLUTE_ZERO_TEN) { 98 mHeavyModel.emplace(tempGear, std::move(config)); 99 } 100 heavyHead = heavyHead->NextSiblingElement(); 101 } 102 } 103 104 XMLElement *defaultList = root->FirstChildElement("default_policy"); 105 if (defaultList != nullptr) { 106 XMLElement *defaultHead = defaultList->FirstChildElement("game_control"); 107 while (defaultHead != 0) { 108 ThermalConfig config; 109 config.fps = defaultHead->IntAttribute("fps", 0); 110 for (auto [coreIndex, cluster] : mIndexToCluster) { 111 std::string clusterStr = "cluster" + std::to_string(coreIndex); 112 int level = defaultHead->IntAttribute(clusterStr.c_str(), -1); 113 if (level >= 0) { 114 config.cpuLevel[cluster] = level; 115 } 116 } 117 int tempGear = defaultHead->IntAttribute("temp", ABSOLUTE_ZERO_TEN); 118 if (tempGear != ABSOLUTE_ZERO_TEN) { 119 mDefaultModel.emplace(tempGear, std::move(config)); 120 } 121 defaultHead = defaultHead->NextSiblingElement(); 122 } 123 } 124 } 125 126 int ThermalControlRecoder::getLimit(ThermalConfig &limit, int temp, bool isHeavy) 127 { 128 if (!mCloudModel.empty()) { 129 return chooseLimit(mCloudModel, limit, temp); 130 } 131 if (isHeavy) { 132 return chooseLimit(mHeavyModel, limit, temp); 133 } 134 return chooseLimit(mDefaultModel, limit, temp); 135 } 136 137 void ThermalControlRecoder::setClusterMap(std::map<int, CLUSTERS> &clusterMap) 138 { 139 mIndexToCluster = clusterMap; 140 mClusterToIndex.clear(); 141 for (auto [index, cluster] : clusterMap) { 142 mClusterToIndex[cluster] = index; 143 } 144 } 145 146 void ThermalControlRecoder::setCloudModel(ThermalModel &model) 147 { 148 mCloudModel = model; 149 } 150 151 void ThermalControlRecoder::clearCloudModel() 152 { 153 mCloudModel.clear(); 154 } 155 156 std::string ThermalControlRecoder::getModel(bool isHeavy) 157 { 158 if (!mCloudModel.empty()) { 159 return printModel(mCloudModel); 160 } 161 if (isHeavy) { 162 return printModel(mHeavyModel); 163 } 164 return printModel(mDefaultModel); 165 } 166 167 int ThermalControlRecoder::chooseLimit(ThermalModel &model, ThermalConfig &limit, int temp) 168 { 169 auto iter = model.lower_bound(temp); 170 if (iter != model.end()) { 171 limit = iter->second; 172 return iter->first; 173 } 174 return ABSOLUTE_ZERO_TEN; 175 } 176 177 std::string ThermalControlRecoder::printModel(ThermalModel &model) 178 { 179 if (model.empty()) { 180 return std::string(); 181 } 182 Json::Value root; 183 for (auto iter = model.rbegin(); iter != model.rend(); iter++) { 184 Json::Value config; 185 config["fps"] = iter->second.fps; 186 for (auto [cluster, level] : iter->second.cpuLevel) { 187 if (mClusterToIndex.find(cluster) == mClusterToIndex.end()) { 188 continue; 189 } 190 std::string clusStr = "cluster" + std::to_string(mClusterToIndex[cluster]); 191 config[clusStr] = level; 192 } 193 root[std::to_string(iter->first)] = config; 194 } 195 return GPA::jsonToString(root); 196 }
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值