使用 TinyXML 解析 XML 元素

发布于 2024-11-18 02:14:40 字数 1289 浏览 3 评论 0原文

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

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

。弄清楚如何拉出个人谷歌搜索后,我还没有找到我的具体示例,所以也许使用过 TinyXML 的人可以帮忙。下面是 XML 的一部分,以及我开始解析它的地方

。 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);
    }
}

我想取出每个卡“类型”并将其保存到一个字符串中以放入组合框中如何访问。这个属性成员?

UPDATE: Still not working :( I have updated the code portion to reflect what I currently have.

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).

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>

Loading/parsing code:

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);
    }
}

I want to pull out each card "type" and save it to a string to put into a combobox. How do I access this attribute member?

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

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

发布评论

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

评论(3

不气馁 2024-11-25 02:14:40

经过大量修改代码后,这是解决方案! (在此处的帮助下)

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;
}

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;
}
空心空情空意 2024-11-25 02:14:40

应该有一个将属性名称作为参数的属性方法,请参阅:http://www.grinninglizard。 com/tinyxmldocs/classTiXmlElement.html

从文档中我看到代码看起来像:

    hRoot.FirstChildElement("card").ToElement()->Attibute("type");

但是,对于您正在做的事情的类型,如果可能的话,我会使用 XPATH。我从未使用过它,但如果您选择走这条路线,TinyXPath 项目可能会有所帮助,链接为: http://tinyxpath .sourceforge.net/

希望这有帮助。

我用来帮助您的文档位于:http://www.grinninglizard.com/ tinyxmldocs/hierarchy.html

there should be a Attribute method that takes and attribut name as parameter see: http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html

from the documentation I see the code would look like:

    hRoot.FirstChildElement("card").ToElement()->Attibute("type");

However for the type of thing you are doing I would use XPATH if at all possible. I have never used it but the TinyXPath project may be helpful if you choose to go that route the link is: http://tinyxpath.sourceforge.net/

Hope this helps.

The documentation I am using to help you from is found at: http://www.grinninglizard.com/tinyxmldocs/hierarchy.html

丶视觉 2024-11-25 02:14:40

您需要的是从元素 card 获取属性 type。所以在你的代码中它应该是这样的:

const char * attribval = hRoot.FirstChild("card").ToElement()->Attribute("card");

What you need is to get the attribute type from the element card. So in your code it should be something like:

const char * attribval = hRoot.FirstChild("card").ToElement()->Attribute("card");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文