如何将 xml 粘贴到 C++ (小XML)

发布于 2024-12-08 04:42:56 字数 1810 浏览 1 评论 0原文


我目前正在开发一个 C++ 项目,我需要从 xml 文件中读取一些内容,我发现tinyxml接缝是可行的方法,但我仍然不知道到底该怎么做。< br> 另外,我的 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”和“最佳成绩”。

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

我的问题是

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

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

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

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

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

I'm currently working on a project in C++ where I need to read some things from a xml file, I've figured out that tinyxml seams to be the way to go, but I still don't know exactly how to do.
Also my xml file is a little tricky, because it looks a little different for every user that needs to use this.

The xml file I need to read looks like this

<?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>

Now the goal of this program is to be able to insert some string (via. a variable) and serch for the corresponding "cScenarioCarrer d:id" and read the "IsComplete" and the "BestScore".

Those strings later need to be worked with in my program, but that I can handle.

My questions here are

A. How do I go by searching for a specific "cScenarioCareer" ID

B. How do I paste the "IsComplete" and "BestScore" into some variables in my program.

Note: The xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx string is unique for every user, so keep in mind it can be anything.

If anyone out there would like to help me, I'd be very graceful, thank you.

PS. I'd like to have some kind of understanding for what I'm doing here, all though "paste this code into your program" answers are acceptable, I think it would be much better if you can tell me how and why it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2024-12-15 04:42:56

由于您是在 C++ 中执行此操作,因此我将使用 ticpp 接口来制作此示例
TinyXml 可从 ticpp.googlecode.com 获取。

假设:

  • 给定的 xml 文件将包含一个 标签和多个
    标签。
  • 每个 包含一个 标记,该标记又包含一个 标记
  • 您已解析了将 xml 文件转换为名为 xmlDoc 的 TiXmlDocument
  • 您不需要检查数据类型属性
  • 您不介意使用异常

我还假设您有一个上下文变量某处包含一个指向
标签,如下所示:

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
}

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

Since you're doing this in C++ I'll make this example using the ticpp interface to
TinyXml that available at ticpp.googlecode.com.

Assumptions:

  • A given xml file will contain one <cloud> tag and multiple
    <cCareerModel> tags.
  • Each <cCareerModel> contains a single <ScenarioCareer> tag which in turn contains a single <cScenarioCareer> tag
  • You've parsed the xml file into a TiXmlDocument called xmlDoc
  • You don't need to examine the data type attributes
  • You don't mind using exceptions

I'll also assume that you have a context variable somewhere containing a pointer to the
<cloud> tag, like so:

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

Here's a function that will locate the ticpp::Element for the cScenarioCareer with
the given 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;
}

Then to get at the information you want you'd do something like:

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
}

Naturally I haven't compiled or tested any of this, but it should give you the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文