gpt4 book ai didi

c++ - 检索元素

转载 作者:行者123 更新时间:2023-11-28 07:48:26 25 4
gpt4 key购买 nike

我是 XML 的初学者。我正在使用 libxml。我创建了一个 XML 文件,如下所示:

<example>

<Path Name="one">Properties/one</Path>
<Path Name="two">Properties/two</Path>
<Path Name="three">Properties/three</Path>
<Path Name="four">Properties/four</Path>

</example>

我的问题是如何获取例如 Properties/one for one 这是路径的名称. ?

谢谢。

最佳答案

快速而肮脏的你可以这样做:

std::string strRetVal;
xmlDocPtr pXMLDoc = xmlParseFile("filename.xml"); // read the xml file
xmlNodePtr rootNode = xmlDocGetRootElement(pXMLDoc); // get the root node (<example>)
xmlNodePtr pNode = rootNode->children;
while (pNode)
{ // walk through all children nodes
if (pNode->type == XML_ELEMENT_NODE)
{
std::string strElemName((char *)pNode->name)); // find all <Path> elements
if (strElemName == "Path")
{
xmlAttrPtr pAttr = m_pXMLNode->properties;
while (pAttr)
{ // walk through all the attributes and find the required one
if (pAttr->type == XML_ATTRIBUTE_NODE)
{
str::string strAttrName((char *)pAttr->name);
str::string strAttrVal((char *)pAttr->children->content);
if ((strAttrName == "Name") && (strAttrVal == "one")) break; // found
}
pAttr = pAttr->next;
}
}
}
pNode = pNode->next;
}
if (pNode)
{ // pNode is the element with an attribute "Name" of value "one"
strRetVal = (char*)xmlNodeGetContent(pNode); // get its content (/Properties/one)
}

关于c++ - 检索元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14361818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com