参考网站:html
https://www.cnblogs.com/haomiao/p/5041065.htmlnode
https://blog.csdn.net/iot_change/article/details/8496977c++
https://blog.csdn.net/sinat_35121480/article/details/54728594函数
待解析文件:网站
inputfile="StairWithRoomWithID.gml" <bldg:interiorRoom> <bldg:Room gml:id="2dQFggKBb1fOc1CqZDIDlx"> <gen:stringAttribute name="ID"> <gen:value>2dQFggKBb1fOc1CqZDIDlx</gen:value> </gen:stringAttribute> <gen:stringAttribute name="storey"> <gen:value>Level-2</gen:value> </gen:stringAttribute> <bldg:lod4MultiSurface> <gml:MultiSurface> <gml:surfaceMember> <gml:CompositeSurface> <gml:surfaceMember> <gml:Polygon gml:id="c4ee18347d0a489d8b5ba816ab6d046d"> <gml:exterior> <gml:LinearRing> <gml:posList srsDimension="3">11.7 9.7 2.7 0.29999999999999893 9.7 2.7 0.29999999999999893 9.7 6.7 11.7 9.7 6.7 11.7 9.7 2.7</gml:posList> </gml:LinearRing> </gml:exterior> </gml:Polygon> </gml:surfaceMember> <gml:surfaceMember> ......
一、load_file(filename)
加载xml文件.net
pugi::xml_document doc; if (!doc.load_file(ifile.c_str())) { cout << "Input file not found"; } // pugi::xml_document做为文档类也做为DOM树的根节点类
二、select_nodes(str)
查找节点code
localise(ss)="*[local-name(.) = '" + s + "']" ss = "//" + localise("Room") + "[@" + localise("id") + "]"; pugi::xpath_node_set roomsolid = doc.select_nodes(ss.c_str()); //查找出全文中 有room名字且有id的 节点集,即文件中全部的<bldg:Room gml:id="2dQFggKBb1fOc1CqZDIDlx">的节点
三、node().attribute("gml:id").value()
查找节点属性为"gml:id"的值xml
map<std::string, pugi::xpath_node> solid_id_node; for (pugi::xpath_node_set::const_iterator it = roomsolid.begin(); it != roomsolid.end(); ++it) { solid_id_node[it->node().attribute("gml:id").value()] = *it; //将该节点的属性**gml:id**的id值,做为键值对中的键;而结点做为值 }
四、nsolid.second.node().name()
输出节点的结点名称htm
for (auto& nsolid:solid_id_node) string ss = nsolid.second.node().name();//输出该节点的节点名称 //nsolid是一个键值对["id","结点"],即找出<bldg:Room gml:id="2dQFggKBb1fOc1CqZDIDlx">,中的"bldg:Room"
五、str1.find_first_of(str2)
提取上点中结点name中的有效值,如Room。该方法继承与String,返回的是从串str1中查找时str2,任何一个首次在str1中出现的位置,与find()
方法有所区别blog
string semantic; std::size_t foundsempos = ss.find_first_of(":");//返回位置冒号的位置 if (foundsempos != std::string::npos) //pos为查找起始位置 { semantic = ss.substr(foundsempos + 1);//提取冒号后的str } else { semantic = ss; }
六、node().first_child()
找出当前node下的第一个子节点
pugi::xpath_node npo1 = nsolid.second.node().first_child();//找出第一个子节点 //结果为 <gen:stringAttribute name="ID">的结点
七、semantic = npo.node().text().as_string()
找出当前节点标签中的值,即
八、node().next_sibling()
找出当前节点同级的下一个节点;
九、pop_back()
向量容器vector的成员函数pop_back()能够删除最后一个元素。
十、./,../,/区别
./是当前目录;
../是父级目录
/是根目录
NOTE:
pugi::xpath_node
和pugi::xml_node
用法的区别node().value
的用法还不是很懂,输出不了值??auto& nsolid:solid_id_node
后者是set集合,前者会变成pair