使用 TinyXML 解析 XML 元素
更新:仍然无法工作:(我已经更新了代码部分以反映我当前拥有的内容。
对于使用过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过大量修改代码后,这是解决方案! (在此处的帮助下)
After a lot of playing around with the code, here is the solution! (With help from HERE)
应该有一个将属性名称作为参数的属性方法,请参阅:http://www.grinninglizard。 com/tinyxmldocs/classTiXmlElement.html
从文档中我看到代码看起来像:
但是,对于您正在做的事情的类型,如果可能的话,我会使用 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:
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
您需要的是从元素
card
获取属性type
。所以在你的代码中它应该是这样的:What you need is to get the attribute
type
from the elementcard
. So in your code it should be something like: