1

我有一个 XML 文件,如下所示:

<?xml version="1.0" encoding="UTF-16"?>
<Table>
   <Dataset>
      <Year>Year1</Year>
      <Month>Month1</Month>
      <Day>Day1</Day>
   </Dataset>
   <Dataset>
      <Year>Year2</Year>
      <Month>Month2</Month>
      <Day>Day1</Day>
   </Dataset>
</Table>


我想用 C++ 读取这个文件。我的代码如下所示:

XMLElement* xeTable = xeExport->FirstChildElement("Table");
     XMLElement* xeDataset = xeTable->FirstChildElement("Dataset");
         XMLElement* xeYear = xeDataset->FirstChildElement("Year");
         XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
         XMLElement* xeDay = xeDataset->FirstChildElement("Day");

             XMLText* xnYear  = xeYear->FirstChild()->ToText();
             const char* cYear = xnYear->Value();

             XMLText* xnMonth  = xeMonth->FirstChild()->ToText();
             const char* cMonth = xnMonth->Value();

             XMLText* xnDay  = xeDay->FirstChild()->ToText();
             const char* cDay = xnDay->Value(); 

它读取第一个数据集的年、月和日期。要知道什么,读取下一个数据集的数据?我在读取后删除了第一个数据集,因此我可以使用 FirstChildElement(); 再次读取第二个数据集。但我没明白。

任何人都可以帮忙吗?

4

1 回答 1

1
xeDataset = xeDataset->NextSiblingElement("Dataset")

to elaborate:

XMLElement* xeTable = xeExport->FirstChildElement("Table");

for(XMLElement* xeDataset = xeTable->FirstChildElement("Dataset"); xeDataset; xeDataset = xeDataset->NextSiblingElement("Dataset"))
{
         XMLElement* xeYear = xeDataset->FirstChildElement("Year");
         XMLElement* xeMonth = xeDataset->FirstChildElement("Month");
         XMLElement* xeDay = xeDataset->FirstChildElement("Day");

         XMLText* xnYear  = xeYear->FirstChild()->ToText();
         const char* cYear = xnYear->Value();

         XMLText* xnMonth  = xeMonth->FirstChild()->ToText();
         const char* cMonth = xnMonth->Value();

         XMLText* xnDay  = xeDay->FirstChild()->ToText();
         const char* cDay = xnDay->Value(); 
}
于 2012-06-06T09:21:29.233 回答