【C++】开源:tinyxml2解析库配置使用

😏*★,°* :.☆( ̄▽ ̄)/$:.°★ 😏

这篇文章主要介绍tinyxml2解析库配置使用。
无专精则不能成,无涉猎则不能通。------梁启超

欢迎来到我的博客,一起学习,共同进步。

喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

    • [:smirk:1. 项目介绍](#:smirk:1. 项目介绍)
    • [:blush:2. 环境配置](#:blush:2. 环境配置)
    • [:satisfied:3. 使用说明](#:satisfied:3. 使用说明)

😏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. 环境配置

下面进行环境配置:

bash 复制代码
sudo apt-get install build-essential
git clone https://github.com/leethomason/tinyxml2.git
cd tinyxml2
make
sudo make install
# 查看版本
pkg-config --modversion tinyxml2

g++编译:g++ -o main main.cpp -ltinyxml2

😆3. 使用说明

下面进行使用分析:

一个解析示例:

cpp 复制代码
#include <iostream>
#include <tinyxml2.h>

int 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出来。

下面是一些解析示例:

cpp 复制代码
// 读取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 node

  while (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();
  }

  // 创建搜索树
  vector<Point> 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]));
    }
  }
  // cout<<"centerpoint size "<<centerLintePoints.size()<<endl;
  tree->read_in(centerLintePoints);
  // cout<<"centerpoint size "<<centerLintePoints.size()<<endl;
  // cout<<"创建搜索树成功"<<endl;
  // printRoad();

  return true;
}
cpp 复制代码
// 解析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;
   // lanesection
   while (laneSection != NULL)
   {
      paserLaneSection(laneSection, road);
      laneSection_id++;
      laneSection = laneSection->NextSiblingElement("laneSection");
   }

   // 解析stopline
   paserStopLineCrosswalk(roadNode, road);

   road->length = road->getRoadLength();
   roadList.push_back(road);
   roadMap[road->road_id] = road;

   return true;
}

gps转xyz工具部分:

cpp 复制代码
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 off
  rot << -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 on

  Eigen::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;
}

以上。

相关推荐
白拾5 分钟前
使用Conda管理python环境的指南
开发语言·python·conda
咖啡里的茶i23 分钟前
C++之继承
c++
从0至131 分钟前
力扣刷题 | 两数之和
c语言·开发语言
总裁余(余登武)31 分钟前
算法竞赛(Python)-万变中的不变“随机算法”
开发语言·python·算法
NormalConfidence_Man32 分钟前
C++新特性汇总
开发语言·c++
一个闪现必杀技37 分钟前
Python练习2
开发语言·python
有梦想的咕噜1 小时前
target_link_libraries()
开发语言
风清扬_jd1 小时前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
liu_chunhai1 小时前
设计模式(3)builder
java·开发语言·设计模式
姜学迁1 小时前
Rust-枚举
开发语言·后端·rust