0


我目前正在使用 C++ 开发一个项目,我需要从 xml 文件中读取一些内容,我发现 tinyxml 接缝是要走的路,但我仍然不知道该怎么做。
此外,我的 xml 文件有点棘手,因为对于每个需要使用它的用户来说,它看起来都有些不同。

我需要阅读的 xml 文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<cloud_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xmlns:d="http://www.kuju.com/TnT/2003/Delta" d:version="1.0">
    <cCareerModel d:id="154964152">
        <ScenarioCareer>
            <cScenarioCareer d:id="237116344">
                <IsCompleted d:type="cDeltaString">CompletedSuccessfully</IsCompleted>
                <BestScore d:type="sInt32">0</BestScore>
                <LastScore d:type="sInt32">0</LastScore>
                <ID>
                    <cGUID>
                        <UUID>
                            <e d:type="sUInt64">5034713268864262327</e>
                            <e d:type="sUInt64">2399721711294842250</e>
                        </UUID>
                        <DevString d:type="cDeltaString">0099a0b7-e50b-45de-8a85-85a12e864d21</DevString>
                    </cGUID>
                </ID>
            </cScenarioCareer>
        </ScenarioCareer>
        <MD5 d:type="cDeltaString"></MD5>
    </cCareerModel>
</cloud_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>

现在这个程序的目标是能够插入一些字符串(通过变量)和搜索相应的“cScenarioCarrer d:id”并读取“IsComplete”和“BestScore”。

这些字符串稍后需要在我的程序中使用,但我可以处理。

我的问题是

A. 如何搜索特定的“cScenarioCareer”ID

B. 如何将“IsComplete”和“BestScore”粘贴到程序中的一些变量中。

注意:xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 字符串对于每个用户都是唯一的,所以请记住它可以是任何东西。

如果有人愿意帮助我,我会非常优雅,谢谢。

PS。我想对我在这里所做的事情有所了解,尽管“将此代码粘贴到您的程序中”的答案是可以接受的,但我认为如果您能告诉我它是如何工作以及为什么工作会更好。

4

1 回答 1

1

由于您在 C++ 中执行此操作,因此我将使用 ticpp.googlecode.com 上提供的 TinyXml 的 ticpp 接口来制作此示例。

假设:

  • 给定的 xml 文件将包含一个<cloud>标签和多个 <cCareerModel>标签。
  • 每个都<cCareerModel>包含一个<ScenarioCareer>标签,而标签又包含一个<cScenarioCareer>标签
  • 您已将 xml 文件解析为名为 xmlDoc 的 TiXmlDocument
  • 您无需检查数据类型属性
  • 你不介意使用异常

我还将假设您在某处有一个上下文变量,其中包含指向 <cloud>标记的指针,如下所示:

ticpp::Element* cloud = xmlDoc.FirstChildElement("cloud");

这是一个函数,它将定位具有给定 ID 的 cScenarioCareer 的 ticpp::Element。

ticpp::Element* findScenarioCareer(const std::string& careerId)
{
  try
  {
    // Declare an iterator to access all of the cCareerModel tags and construct an
    // end iterator to terminate the loop
    ticpp::Iterator<ticpp::Element> careerModel;
    const ticpp::Iterator<ticpp::Element> modelEnd = careerModel.end();

    // Loop over the careerModel tags
    for (careerModel = cloud->FirstChildElement() ; careerModel != modelEnd ;
         ++careerModel)
    {
      // Construct loop controls to access careers
      ticpp::Iterator<ticpp::Element> career;
      const ticpp::Iterator<ticpp::ELement> careerEnd = career.end();

      // Loop over careers
      for (career = careerModel->FirstChildElement("ScenarioCareer").FirstChildElement() ; 
           career != careerEnd ; ++career)
      {
        // If the the d:id attribute value matches then we're done
        if (career->GetAttributeOrDefault("d:id", "") == careerId)
          return career;
      }
    }
  }
  catch (const ticpp::Exception&)
  {
  }
  return 0;
}

然后要获取您想要的信息,您可以执行以下操作:

std::string careerId = "237116344";
std::string completion;
std::string score;
ticpp::Element* career = findScenarioCareer(careerId);
if (career)
{
  try
  {
    completion = career->FirstChildElement("IsCompleted")->GetText();
    score = career->FirstChildElement("BestScore")->GetText();
  }
  catch (const ticpp::Exception&)
  {
    // Handle missing element condition
  }
}
else
{
  // Not found
}

当然,我还没有编译或测试过任何这些,但它应该给你这个想法。

于 2011-10-01T18:20:06.570 回答