【问题标题】:How to paste xml to C++ (Tinyxml)如何将 xml 粘贴到 C++ (Tinyxml)
【发布时间】:2011-10-01 15:16:44
【问题描述】:


我目前正在使用 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”粘贴到程序中的一些变量中。

注意:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 字符串对于每个用户都是唯一的,因此请记住它可以是任何内容。

如果有人愿意帮助我,我会非常感激,谢谢。

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

【问题讨论】:

标签: c++ xml tinyxml


【解决方案1】:

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

假设:

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

我还假设您在某处有一个上下文变量,其中包含指向 &lt;cloud&gt; 标签,像这样:

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

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

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
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多