首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索元素

检索元素
EN

Stack Overflow用户
提问于 2013-01-16 15:24:18
回答 1查看 270关注 0票数 0

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

代码语言:javascript
复制
<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,例如,one,这是路径的名称。?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-16 16:03:55

又快又脏你可以这样做:

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14361818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档