我自己封装tinyxml2的库形成了一个解析xmld的类,分享在此,欢迎与我交流。 tinyxml2是一个解析xml的轻型库,为了方便以后的使用,我自己封装了一个工具类。 某些格式转换的代码比如string和utf-8互转的代码可以根据自己的需要进行实现。 tinyxml2库下载:https://github.com/leethomason/tinyxml2
//tinyXmlPlus.h文件 #pragma once #include "tinyxml2-master\\tinyxml2.h" class CTineyXmlPlus { public: CTineyXmlPlus(std::string strXmlPath); ~CTineyXmlPlus(); public: tinyxml2::XMLDocument* GetMyDocument(); //获取xml对象 tinyxml2::XMLElement *GetRootElement(); //获取根节点 void initDocument(); //初始化文档 public: //按xml表的顺序遍历全部节点 bool ReadAllXmlValue(std::vector<std::string>& vectorValue); //读取全部的一级节点 bool ReadAllFirstLevelValue(std::vector<std::string>& vectorValue); //读取某个一级节点下的全部子节点 bool ReadSpecifyChildValue(std::string strFatherNodeValue, std::vector<std::string>& vectorValue); protected: tinyxml2::XMLDocument m_mydocument; //声明xml对象 tinyxml2::XMLElement *m_pRootElement; //根节点 tinyxml2::XMLElement *m_pSurface; //节点入口,获取第一个值为value的子节点 默认为空则返回第一个节点 std::vector<std::string> m_vectorValue; std::vector<std::string> ReadXml(tinyxml2::XMLElement * pSurface); }; //tinyXmlPlus.cpp文件 #include "stdafx.h" #include "TineyXmlPlus.h" #include "common.h" using namespace tinyxml2; CTineyXmlPlus::CTineyXmlPlus(std::string szFilePathAndName) :m_vectorValue(NULL) { m_mydocument.LoadFile(szFilePathAndName.c_str()); //载入xml文件 m_pRootElement = m_mydocument.RootElement(); //根节点赋值 m_pSurface = m_pRootElement->FirstChildElement();//获取第一个值为value的子节点 默认为空则返回第一个节点 } CTineyXmlPlus::~CTineyXmlPlus() { m_mydocument.Clear();// Clear the document, resetting it to the initial state. } tinyxml2::XMLDocument* CTineyXmlPlus::GetMyDocument() { return &m_mydocument; } XMLElement * CTineyXmlPlus::GetRootElement() { return m_pRootElement; } void CTineyXmlPlus::initDocument() { m_mydocument.Clear(); } bool CTineyXmlPlus::ReadAllXmlValue(std::vector<std::string>& vectorValue) { m_pSurface = m_pRootElement->FirstChildElement();//获取第一个值为value的子节点 默认为空则返回第一个节点 vectorValue = ReadXml(m_pSurface); return true; } bool CTineyXmlPlus::ReadAllFirstLevelValue(std::vector<std::string>& vectorValue) { //若出现乱码,请使用str = utf8ToCString(value.c_str());进行转码 m_pSurface = m_pRootElement->FirstChildElement();//获取第一个值为value的子节点 默认为空则返回第一个节点 while (m_pSurface) { std::string cStr; const XMLAttribute *attr = m_pSurface->FirstAttribute();//获取第一个属性值 while (attr) { vectorValue.push_back(cStringToString(utf8ToCString(attr->Value())));//此处为utf8格式,若显示乱码,请手动转码。 attr = attr->Next(); //获取下一个属性值 } m_pSurface = m_pSurface->NextSiblingElement();//当前对象下方的兄弟节点 } return true; } bool CTineyXmlPlus::ReadSpecifyChildValue(std::string strFatherNodeValue, std::vector<std::string>& vectorValue) { //若出现乱码,请使用str = utf8ToCString(value.c_str());进行转码 m_pSurface = m_pRootElement->FirstChildElement();//获取第一个值为value的子节点 默认为空则返回第一个节点 while (m_pSurface) { std::string cStr; const XMLAttribute *attr = m_pSurface->FirstAttribute();//获取第一个属性值 while (attr) { if (cStringToString(utf8ToCString(attr->Value())) == strFatherNodeValue) { m_pSurface = m_pSurface->FirstChildElement(); while (m_pSurface) { std::string cStr; const XMLAttribute *attr = m_pSurface->FirstAttribute();//获取第一个属性值 while (attr) { vectorValue.push_back(cStringToString(utf8ToCString(attr->Value())));//此处为utf8格式,若显示乱码,请手动转码。 attr = attr->Next(); //获取下一个属性值 } m_pSurface = m_pSurface->NextSiblingElement();//当前对象下方的兄弟节点 } return true; } attr = attr->Next(); //获取下一个属性值 } m_pSurface = m_pSurface->NextSiblingElement();//当前对象下方的兄弟节点 } return true; } std::vector<std::string> CTineyXmlPlus::ReadXml(XMLElement * pSurface) { //若出现乱码,请使用str = utf8ToCString(value.c_str());进行转码 while (pSurface) { std::string cStr; const XMLAttribute *attr = pSurface->FirstAttribute();//获取第一个属性值 while (attr) { m_vectorValue.push_back(cStringToString(utf8ToCString(attr->Value())));//此处为utf8格式,若显示乱码,请手动转码。 attr = attr->Next(); //获取下一个属性值 } // const char* content = m_pSurface->GetText(); //获取节点的内容 XMLElement *surface1 = pSurface->FirstChildElement(); //查看当前对象是否有子节点 if (surface1) { ReadXml(surface1);//递归调用 } pSurface = pSurface->NextSiblingElement();//当前对象的下一个节点 } return m_vectorValue; }xml文件格式如下
<root> <a name="山东"> <a name="淄博"> <a name="淄博" /> <a name="淄川" /> <a name="张店" /> <a name="博山" /> <a name="临淄" /> <a name="周村" /> <a name="桓台" /> <a name="高青" /> <a name="沂源" /> </a> <a name="菏泽"> <a name="菏泽" /> <a name="牡丹区" /> <a name="曹县" /> <a name="定陶" /> <a name="成武" /> <a name="单县" /> <a name="巨野" /> <a name="郓城" /> <a name="鄄城" /> <a name="东明" /> </a> </a> <a name="河北"> <a name="石家庄"> <a name="石家庄" /> <a name="长安" /> <a name="桥西" /> <a name="新华" /> <a name="裕华区" /> <a name="井陉矿区" /> <a name="井陉县" /> </a> </a> </root>示例代码如下
CTineyXmlPlus *xmlTemp = new CTineyXmlPlus("此处填写xml路所在径"); std::vector<std::string> vecTemp; bool isOk = xmlTemp->ReadAllXmlValue(vecTemp);//读取全部节点数据 for (auto &r : vecTemp) { cout<<r<<endl; }