大朗网站制作,apache 多网站,网站设计收费明细表,wordpress mp4播放器#x1f60f;★,:.☆(#xffe3;▽#xffe3;)/$:.★ #x1f60f; 这篇文章主要介绍tinyxml2解析库配置使用。 无专精则不能成#xff0c;无涉猎则不能通。——梁启超 欢迎来到我的博客#xff0c;一起学习#xff0c;共同进步。 喜欢的朋友可以关注一下#xff0c;… ★,°:.☆(▽)/$:.°★ 这篇文章主要介绍tinyxml2解析库配置使用。 无专精则不能成无涉猎则不能通。——梁启超 欢迎来到我的博客一起学习共同进步。 喜欢的朋友可以关注一下下次更新不迷路 文章目录 :smirk:1. 项目介绍:blush:2. 环境配置:satisfied:3. 使用说明一个解析示例xml地图解析 1. 项目介绍
项目Github地址https://github.com/leethomason/tinyxml2
TinyXML-2是一个轻量级的C库用于解析和生成XML文档。它是对原始TinyXML库的改进和扩展提供了更快速、更强大的XML处理功能。
以下是一些TinyXML-2的主要特点和功能 1.简单易用TinyXML-2提供了简单的API使得解析和生成XML文档变得简单和直观。它使用类似于DOM文档对象模型的方法来操作XML元素让开发者可以轻松地读取和写入XML数据。 2.轻巧高效TinyXML-2具有非常小的内存占用和高性能。它专注于简单的XML操作没有复杂的依赖关系因此可以快速加载和处理大型XML文件。 3.支持解析和生成TinyXML-2支持从字符串或文件中解析XML文档并且可以生成格式良好的XML文本。它能够处理各种节点类型如元素、属性、文本、注释等。 4.错误处理TinyXML-2提供了灵活的错误处理机制。当解析XML时它可以检测到语法错误、结构错误或其他问题并提供相关的错误信息和异常处理机制。 5.跨平台TinyXML-2可以在多个操作系统上使用包括Windows、Linux和Mac OS等。 2. 环境配置
下面进行环境配置
sudo apt-get install build-essential
git clone https://github.com/leethomason/tinyxml2.git
cd tinyxml2
make
sudo make install
# 查看版本
pkg-config --modversion tinyxml2g编译g -o main main.cpp -ltinyxml2
3. 使用说明
下面进行使用分析
一个解析示例
#include iostream
#include tinyxml2.hint main() {// 创建一个XML文档对象tinyxml2::XMLDocument doc;// 加载XML文件if (doc.LoadFile(example.xml) tinyxml2::XML_SUCCESS) {// 打印根元素名称tinyxml2::XMLElement* root doc.FirstChildElement(Root);if (root) {std::cout Root Element: root-Name() std::endl;}// 遍历并打印所有子元素tinyxml2::XMLElement* element root-FirstChildElement();while (element) {std::cout Element Name: element-Name() std::endl;// 获取元素的属性值const char* attributeValue element-Attribute(attribute);if (attributeValue) {std::cout Attribute Value: attributeValue std::endl;}// 获取元素的文本内容const char* textValue element-GetText();if (textValue) {std::cout Text Value: textValue std::endl;}element element-NextSiblingElement();}}// 创建一个新的XML文档tinyxml2::XMLDocument newDoc;// 创建根元素tinyxml2::XMLElement* newRoot newDoc.NewElement(NewRoot);newDoc.InsertFirstChild(newRoot);// 创建子元素tinyxml2::XMLElement* newElement newDoc.NewElement(NewElement);newRoot-InsertEndChild(newElement);// 设置属性值newElement-SetAttribute(attribute, value);// 设置文本内容newElement-SetText(Hello, World!);// 保存XML文件newDoc.SaveFile(new_example.xml);return 0;
}xml地图解析
项目github地址推荐学习https://github.com/chenyongzhe/HdmapEngine
这个地图解析引擎项目用tinyxml2库解析apollo opendrive xml格式的高精地图包含道路、车道连接关系、信号灯等元素以及车道搜索、wgs84转东北天等工具最后可用python matplotlib将处理完的地图show出来。
下面是一些解析示例
// 读取xml文件去判断Node
bool HdmapEngine::paserApolloxml(const char *file_name)
{tinyxml2::XMLDocument doc;if (doc.LoadFile(file_name) ! XML_SUCCESS)return false;XMLElement *root doc.RootElement();XMLElement *roadNode root-FirstChildElement(road); // find road nodewhile (roadNode ! NULL){string name roadNode-Attribute(name); // road name// if name include string(Road) or string(junction)if (name.substr(0, 4) Road){// cout roadNode-Attribute(id) endl;paserRoad(roadNode);}else if (name.substr(0, 8) junction){paserJunction(roadNode);}roadNode roadNode-NextSiblingElement();}// 创建搜索树vectorPoint centerLintePoints;for (int i 0; i laneList.size(); i){// if(laneList[i].centerLinePoints!NULL)for (int j 0; j laneList[i]-centerLinePoints.size(); j){centerLintePoints.push_back(*(laneList[i]-centerLinePoints[j]));}}// coutcenterpoint size centerLintePoints.size()endl;tree-read_in(centerLintePoints);// coutcenterpoint size centerLintePoints.size()endl;// cout创建搜索树成功endl;// printRoad();return true;
}// 解析road元素
bool HdmapEngine::paserRoad(XMLElement *roadNode)
{string predecessor_elementType;int predecessor_id INT_MAX;int successor_id INT_MAX;string successor_elementType;double road_length;road_id atoi(roadNode-Attribute(id));XMLElement *linkNode roadNode-FirstChildElement(link);XMLElement *lanes roadNode-FirstChildElement(lanes);XMLElement *laneSection lanes-FirstChildElement(laneSection);XMLElement *sucNode linkNode-FirstChildElement(successor);if (sucNode ! NULL){// cout sucNode-Attribute(elementType) sucNode-Attribute(elementId) endl;successor_elementType sucNode-Attribute(elementType);successor_id atoi(sucNode-Attribute(elementId));}XMLElement *preNode linkNode-FirstChildElement(predecessor);if (preNode ! NULL){// cout preNode-Attribute(elementType) preNode-Attribute(elementId) endl;predecessor_elementType preNode-Attribute(elementType);predecessor_id atoi(preNode-Attribute(elementId));}Road *road new Road(road_id, predecessor_elementType, predecessor_id, successor_elementType, successor_id);int jun atoi(roadNode-Attribute(junction));if (jun -1){// 非路口道路road-isJunctionRoad false;}else{// 路口道路road-isJunctionRoad true;}laneSection_id 0;// lanesectionwhile (laneSection ! NULL){paserLaneSection(laneSection, road);laneSection_id;laneSection laneSection-NextSiblingElement(laneSection);}// 解析stoplinepaserStopLineCrosswalk(roadNode, road);road-length road-getRoadLength();roadList.push_back(road);roadMap[road-road_id] road;return true;
}gps转xyz工具部分
bool TransformUtil::gps2xyz(const double longitude, const double latitude, const double altitude,Eigen::Vector3d xyz)
{// gps gps_msg.longitude, gps_msg.latitude, gps_msg.altitude;Eigen::Vector3d gps(longitude, latitude, altitude);Eigen::Vector3d gps_ECEF WGS84toECEF(gps);// 处理GPS数据double rad_lon gps_origin_[1] / 180 * M_PI;double rad_lat gps_origin_[0] / 180 * M_PI;double sin_lon sin(rad_lon);double cos_lon cos(rad_lon);double sin_lat sin(rad_lat);double cos_lat cos(rad_lat);Eigen::Matrix3d rot Eigen::Matrix3d::Zero();// clang-format offrot -sin_lon, cos_lon, 0,-sin_lat * cos_lon, -sin_lat * sin_lon, cos_lat,cos_lat * cos_lon, cos_lat * sin_lon, sin_lat;// clang-format onEigen::Vector3d diff_ECEF gps_ECEF - origin_ECEF_;Eigen::Vector3d xyz_ECEF rot * diff_ECEF;xyz xyz_ECEF[0], xyz_ECEF[1], xyz_ECEF[2];return true;
}以上。