c++ - 如何使用 TinyXML 从 XML 文件中读取所有数据

标签 c++ xml tinyxml

我想使用 TinyXML 读取 XML 数据。我有以下 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>

以及以下 C++ 代码:

    for(TiXmlElement* e = elem->FirstChildElement("time"); e != NULL; e = e->FirstChildElement("symbol"))
        {
            cout << "Got symbol" << endl;

            attr = e->Attribute("var");
            if(attr != NULL)
                cout << "Got var " << attr << endl;
            attr = e->Attribute("name");
            if(attr != NULL)
                cout << "Got name " << attr << endl;
                attr = e->Attribute("number");
            if(attr != NULL)
                cout << "Got number " << attr << endl;
        }

我的问题是:如何更改此设置以便读取和显示这三天的所有“符号”数据?

问候,

最佳答案

for 语句的迭代子句是错误的。这需要迭代 time 元素,并且对于每个 time 元素,您必须获取它的子 symbol 元素。

for (TiXmlElement* e = elem->FirstChildElement("time"); e != NULL; e = e->NextSiblingElement("time"))
{
    TiXmlElement* t = e->FirstChildElement("symbol");
    if (t)
    {
        const char *a1 = t->Attribute("var");
        const char *a2 = t->Attribute("name");
        const char *a3 = t->Attribute("number");

        if (a1)
            cout << "Got var " << a1 << "\n";
        if (a2)
            cout << "Got name " << a2 << "\n";
        if (a3)
            cout << "Got number " << a3 << "\n";
    }
}

关于c++ - 如何使用 TinyXML 从 XML 文件中读取所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489017/

相关文章:

c++ - 重新抛出具有相同消息的新异常会导致垃圾输出。为什么?

c++ - 我怎么知道在 C++ 中排序的 vector 是否有重复值

java - 使用这个在构造函数中设置变量

java - 尝试多线程 URL 连接以减少加载时间

ios - 解析 XML 时如何获取特定元素?

c++ - 我应该使用 XPath 还是只使用 DOM?

c++ - 如何在 C++ 中使用 FMOD?

xml - 基于标签名称及其值的知识计算 XML 文档的 XPath

c++ - const char* 成员

c++ - 在 C++ 中使用 tiny xml 进行 xml 解析的问题?