使用 TinyXML 读取所有兄弟元素
Reading in all sibling's elements with TinyXML
我在读取 XML 文件时遇到问题!下面的代码用于从 XML 文件中获取 children 的值(国家),但是我需要遍历所有兄弟姐妹(所有三个国家)并且我需要一个解决方案,它也适用于无限数量的兄弟姐妹。我的 xml 文件如下所示:
<?xml version="1.0"?>
<data>
<country name ="Liechteinstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我有这样的代码:
XMLDocument doc;
doc.LoadFile("test.xml");
tinyxml2::XMLElement* countryElement = doc.FirstChildElement("data")->FirstChildElement("country");
ofstream myfile;
myfile.open("result.txt", ios::out | ios::app );
for (tinyxml2::XMLElement* child = countryElement->NextSiblingElement()->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
{
myfile << child->GetText() << endl;
}
myfile.close();
这会读取第一个国家/地区的所有值,但我需要读取所有三个国家/地区的值并将其写入 txt 文件。我尝试使用 for cycle 来遍历添加
的兄弟姐妹
countryElement = countryElement->NextSiblingElement()
在for循环的最后,但是没有成功。有人可以帮我吗?提前致谢!
我建议使用两个循环。一个循环遍历国家,一个循环遍历内部的邻居:
int main() {
tinyxml2::XMLDocument doc;
doc.LoadFile("test.xml");
tinyxml2::XMLElement* dataElement = doc.FirstChildElement("data");
std::ofstream myfile;
myfile.open("result.txt", std::ios::out | std::ios::app);
for (tinyxml2::XMLElement* countryElement = dataElement->FirstChildElement("country");
countryElement != NULL; countryElement = countryElement->NextSiblingElement())
{
myfile << "Country name: " << countryElement->Attribute("name") << std::endl;
myfile << "Rank: " << countryElement->FirstChildElement("rank")->GetText() << std::endl;
myfile << "Year: " << countryElement->FirstChildElement("year")->GetText() << std::endl;
myfile << "gdppc: " << countryElement->FirstChildElement("gdppc")->GetText() << std::endl;
for (tinyxml2::XMLElement* child = countryElement->FirstChildElement("neighbor");
child != NULL; child = child->NextSiblingElement())
{
myfile << "Neighbor: " << child->Attribute("name") << std::endl;
}
myfile << std::endl;
}
myfile.close();
}
输出:
Country name: Liechteinstein
Rank: 1
Year: 2008
gdppc: 141100
Neighbor: Austria
Neighbor: Switzerland
Country name: Singapore
Rank: 4
Year: 2011
gdppc: 59900
Neighbor: Malaysia
Country name: Panama
Rank: 68
Year: 2011
gdppc: 13600
Neighbor: Costa Rica
Neighbor: Colombia
我在读取 XML 文件时遇到问题!下面的代码用于从 XML 文件中获取 children 的值(国家),但是我需要遍历所有兄弟姐妹(所有三个国家)并且我需要一个解决方案,它也适用于无限数量的兄弟姐妹。我的 xml 文件如下所示:
<?xml version="1.0"?>
<data>
<country name ="Liechteinstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我有这样的代码:
XMLDocument doc;
doc.LoadFile("test.xml");
tinyxml2::XMLElement* countryElement = doc.FirstChildElement("data")->FirstChildElement("country");
ofstream myfile;
myfile.open("result.txt", ios::out | ios::app );
for (tinyxml2::XMLElement* child = countryElement->NextSiblingElement()->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
{
myfile << child->GetText() << endl;
}
myfile.close();
这会读取第一个国家/地区的所有值,但我需要读取所有三个国家/地区的值并将其写入 txt 文件。我尝试使用 for cycle 来遍历添加
的兄弟姐妹countryElement = countryElement->NextSiblingElement()
在for循环的最后,但是没有成功。有人可以帮我吗?提前致谢!
我建议使用两个循环。一个循环遍历国家,一个循环遍历内部的邻居:
int main() {
tinyxml2::XMLDocument doc;
doc.LoadFile("test.xml");
tinyxml2::XMLElement* dataElement = doc.FirstChildElement("data");
std::ofstream myfile;
myfile.open("result.txt", std::ios::out | std::ios::app);
for (tinyxml2::XMLElement* countryElement = dataElement->FirstChildElement("country");
countryElement != NULL; countryElement = countryElement->NextSiblingElement())
{
myfile << "Country name: " << countryElement->Attribute("name") << std::endl;
myfile << "Rank: " << countryElement->FirstChildElement("rank")->GetText() << std::endl;
myfile << "Year: " << countryElement->FirstChildElement("year")->GetText() << std::endl;
myfile << "gdppc: " << countryElement->FirstChildElement("gdppc")->GetText() << std::endl;
for (tinyxml2::XMLElement* child = countryElement->FirstChildElement("neighbor");
child != NULL; child = child->NextSiblingElement())
{
myfile << "Neighbor: " << child->Attribute("name") << std::endl;
}
myfile << std::endl;
}
myfile.close();
}
输出:
Country name: Liechteinstein
Rank: 1
Year: 2008
gdppc: 141100
Neighbor: Austria
Neighbor: Switzerland
Country name: Singapore
Rank: 4
Year: 2011
gdppc: 59900
Neighbor: Malaysia
Country name: Panama
Rank: 68
Year: 2011
gdppc: 13600
Neighbor: Costa Rica
Neighbor: Colombia