检索一个元素
Retrieving an element
我是初学者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)
}
相关文章:
- lower_bound()返回最后一个元素
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 仅显示链表的最后一个元素
- push_back通过自行创建的对象获取最后一个元素的向量
- 使用 map.end() 访问 map 的最后一个元素
- 如何知道地图中的最后一个元素是否被删除?
- 选择一个元素而不是一个对象的数组的原因
- std::矢量保存 只推送最后一个元素
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- 反转后不返回最后一个元素
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 访问 for_each 函数中的前一个元素
- 在 QListView 中显示最后一个元素
- 从 std::vector 中选择一个元素,而不是给定元素
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 遍历列表包含排除最后一个元素的内容
- 如何从一个无序集合中获取一个元素