首页 > 代码库 > plist解析, 简易实现.

plist解析, 简易实现.

源码

 1 class Xml { 2 public: 3     typedef std::pair<std::wstring, std::wstring> NodeT; 4     static std::vector<NodeT> parse(const std::wstring &xml) 5     { 6         std::vector<NodeT> result; 7  8         int stackCount = 0; 9         std::wstring tagName;10         std::wstring::const_iterator11             curIter = std::begin(xml),12             begIter = std::end(xml), endIter = std::end(xml), 13             cbegIter = std::end(xml), cendIter = std::end(xml);14 15         std::wsmatch smatch;16         while (std::regex_search(curIter, std::end(xml), smatch, std::wregex(LR"(</\w+>|<\w+>)")))17         {18             if (stackCount == 0)19             {20                 begIter = smatch[0].first;21                 cbegIter = smatch[0].second;22                 tagName = std::regex_replace(smatch[0].str(), std::wregex(LR"(<(\w+)>)"), LR"($1)");23             }24 25             curIter = smatch[0].second;26             if (smatch[0].str().substr(1, tagName.size()) == tagName)27                 ++stackCount;28             else if (smatch[0].str().substr(2, tagName.size()) == tagName)29                 --stackCount;30 31             //    匹配完成.32             if (stackCount == 0)33             {34                 endIter = smatch[0].second;35                 cendIter = smatch[0].first;36 37                 result.push_back(std::make_pair(tagName, std::wstring(cbegIter, cendIter)));38             }39         }40         return result;41     };42 };

效果图

由于项目需要输入, 输出plist文件.

C++标准又没有xml库. 索性就自己写了个简易版.

功能相当有限, 仅支持解析个个标签内容.

不过, 对我来说已经足够了.

代码也不算长, 凑合用用.

 

substr可以优化掉...

plist解析, 简易实现.