C++ 使用 TinyXML 解析 XML 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6521404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:18:36  来源:igfitidea点击:

Parsing XML Elements using TinyXML

c++xmlxml-parsingtinyxml

提问by Jon

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

对于使用过 TinyXML 的人来说,这应该是一个非常简单的问题。我正在尝试使用 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.

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

XML:

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?

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

回答by Jon

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

回答by Rob

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

应该有一个将属性名称作为参数的属性方法,请参见: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/

但是,对于您正在做的事情类型,如果可能的话,我会使用 XPATH。我从未使用过它,但如果您选择走这条路线,TinyXPath 项目可能会有所帮助,链接是: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

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

回答by iceaway

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

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

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