[英]Why does this code produce 4 times nothing and the fifth time the correct data?
我得到了一個 XML 文件:
<weatherdata> <location> <name>Vlaardingen</name> <type/> <country>NL</country> <timezone/> <location altitude="0" latitude="51.912498" longitude="4.34167" geobase="geonames" geobaseid="2745467"/> </location> <credit/> <meta> <lastupdate/> <calctime>0.0152</calctime> <nextupdate/> </meta> <sun rise="2016-02-23T06:40:58" set="2016-02-23T17:11:47"/> <forecast> <time day="2016-02-23"> <symbol number="500" name="lichte regen" var="10d"/> <precipitation/> <windDirection deg="316" code="NW" name="Northwest"/> <windSpeed mps="9.01" name="Fresh Breeze"/> <temperature day="6.06" min="5.57" max="6.06" night="5.66" eve="5.57" morn="6.06"/> <pressure unit="hPa" value="1027.72"/> <humidity value="96" unit="%"/> <clouds value="clear sky" all="8" unit="%"/> </time> <time day="2016-02-24"> <symbol number="501" name="matige regen" var="10d"/> <precipitation value="3.15" type="rain"/> <windDirection deg="283" code="WNW" name="West-northwest"/> <windSpeed mps="6.21" name="Moderate breeze"/> <temperature day="4.98" min="4.17" max="5.11" night="4.17" eve="4.85" morn="4.32"/> <pressure unit="hPa" value="1030.97"/> <humidity value="100" unit="%"/> <clouds value="scattered clouds" all="48" unit="%"/> </time> <time day="2016-02-25"> <symbol number="500" name="lichte regen" var="10d"/> <precipitation value="1.23" type="rain"/> <windDirection deg="295" code="WNW" name="West-northwest"/> <windSpeed mps="5.71" name="Moderate breeze"/> <temperature day="5.43" min="4.92" max="5.48" night="5.34" eve="5.48" morn="4.92"/> <pressure unit="hPa" value="1026.18"/> <humidity value="100" unit="%"/> <clouds value="broken clouds" all="68" unit="%"/> </time> </forecast> </weatherdata>
這是我讀取 XML 文件的 C++ 代碼:
#include <iostream>
#include <string>
#include "tinyxml2.h"
using namespace std;
struct weatherData
{
// date of day
string time_day;
// symbol data for weathericon and display of weather type
string symbol_number;
string symbol_name;
string symbol_var;
// windspeed
string windSpeed_mps;
// min. and max. temperature
string temp_min;
string temp_max;
};
int main()
{
weatherData forecast[3];
int counter = 0;
tinyxml2::XMLDocument doc;
if(doc.LoadFile("daily.xml") == tinyxml2::XML_SUCCESS)
{
tinyxml2::XMLElement* root = doc.FirstChildElement();
for(tinyxml2::XMLElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
{
std::string elemName = elem->Value();
for (tinyxml2::XMLElement* e = elem->FirstChildElement("time"); e != NULL; e = e->NextSiblingElement("time"))
{
if (e)
{
const char *time = e->Attribute("day");
forecast[counter].time_day = time;
counter++;
}
}
cout << "Time dates: " << endl;
for (int i = 0; i < 3;i++)
{
cout << forecast[i].time_day << endl;
}
counter = 0;
}
}
}
我是編碼新手。 我正在使用博客中的示例代碼並根據我的需要對其進行了調整。 我知道 for 循環只是在 XML 文件中的元素上運行。 每次它找到元素 'time' 時,它都會查看它是否具有屬性 'day'。 我不明白的是為什么它運行 4 次,第五次它產生三個“時間”部分的屬性。
這是輸出:
時間日期:
時間日期:
時間日期:
時間日期:
時間日期:
2016-02-23 2016-02-24 2016-02-25
這是因為您的外循環遍歷根元素weatherdata
所有直接后繼,即它遍歷元素節點location
、 credit
、 meta
、 sun
和forecast
。 對於這些元素中的每一個,您都可以搜索您真正感興趣的time
元素。 但是前4個元素,即location
, credit
, meta
和sun
不包含任何time
元素,因此外循環的前4次迭代無法提取任何時間數據,而第5次迭代則選擇元素節點forecast
,即具有您正在尋找的三個time
元素。
我想如果您按如下方式更改代碼,它會起作用(注意調用FirstChildElement
的"forecast"
參數):
....
if(doc.LoadFile("daily.xml") == tinyxml2::XML_SUCCESS)
{
tinyxml2::XMLElement* root = doc.FirstChildElement();
for(tinyxml2::XMLElement* elem = root->FirstChildElement("forecast"); elem != NULL; elem = elem->NextSiblingElement())
{
....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.