使用 TinyXML 解析 XML 元素 [英] Parsing XML Elements using TinyXML

查看:44
本文介绍了使用 TinyXML 解析 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:仍然无法正常工作:(我已经更新了代码部分以反映我目前拥有的内容.

对于使用过 TinyXML 的人来说,这应该是一个非常简单的问题.我正在尝试使用 TinyXML 解析 XML 文档并提取一些值.昨天想通了怎么在库中添加,已经成​​功加载文档了(嘿嘿,开始了).

This should be a pretty easy question for people who have used TinyXML. I'm attempting to use TinyXML to parse through an XML document and pull out some values. I figured out how to add in the library yesterday, and I have successfully loaded the document (hey, it's a start).

我一直在阅读手册,但我不太清楚如何提取单个属性.在谷歌搜索之后,我没有找到我的具体示例的示例,所以也许这里使用过 TinyXML 的人可以提供帮助.下面是 XML 的一部分,我已经开始解析它了.

I've been reading through the manual and I can't quite figure out how to pull out individual attributes. After Googling around, I haven't found an example of my specific example, so perhaps someone here who has used TinyXML can help out. Below is a slice of the XML, and where I have started to parse it.

XML:

<EGCs xmlns="http://tempuri.org/XMLSchema.xsd">
  <card type="EGC1">
    <offsets>
      [ ... ]
    </offsets>
  </card>

   <card type="EGC2">
    <offsets>
      [ ... ]
    </offsets>
  </card>
</EGCs>

加载/解析代码:

TiXmlDocument doc("EGC_Cards.xml");
if(doc.LoadFile())
{
    TiXmlHandle hDoc(&doc);
    TiXmlElement* pElem;
    TiXmlHandle hRoot(0);
    pElem = hDoc.FirstChildElement().Element();
    if (!pElem) return false;
    hRoot = TiXmlHandle(pElem);

    //const char *attribval = hRoot.FirstChild("card").ToElement()->Attribute("card");
    pElem = hDoc.FirstChild("EGCs").Child("card", 1).ToElement();
    if(pElem)
    {
        const char* tmp = pElem->GetText();
        CComboBox *combo = (CComboBox*)GetDlgItem(IDC_EGC_CARD_TYPE);
        combo->AddString(tmp);
    }
}

我想取出每张卡片类型"并将其保存为字符串以放入组合框中.如何访问此属性成员?

推荐答案

在玩了很多代码之后,这里是解决方案!(在 这里的帮助下)

After a lot of playing around with the code, here is the solution! (With help from HERE)

TiXmlDocument doc("EGC_Cards.xml");
combo = (CComboBox*)GetDlgItem(IDC_EGC_CARD_TYPE);

if(doc.LoadFile())
{
    TiXmlHandle hDoc(&doc);
    TiXmlElement *pRoot, *pParm;
    pRoot = doc.FirstChildElement("EGCs");
    if(pRoot)
    {
        pParm = pRoot->FirstChildElement("card");
        int i = 0; // for sorting the entries
        while(pParm)
        {
            combo->InsertString(i, pParm->Attribute("type"));
            pParm = pParm->NextSiblingElement("card");
            i++;
        }
    }
}
else 
{
    AfxMessageBox("Could not load XML File.");
    return false;
}

这篇关于使用 TinyXML 解析 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆