实习随手记-用相关解析器解析XML档

来源:互联网 发布:小米的免费网络短信 编辑:程序博客网 时间:2024/06/08 00:47

<

了解常用的C++xml解析器(这里只提供2种常用的)

http://pugixml.org/

http://www.grinninglizard.com/tinyxml/

https://github.com/leethomason/tinyxml2

xml解析还有Cmarkuprapidxml等这里就不一一提供了,选择一种适合自己的即可,推荐pugixml

>

#include"pugiconfig.hpp"

#include"pugixml.hpp"
#include<stdio.h>
#include<iostream>
#include<tchar.h>
using namespace std;
int main()
{

const std::wstring strFilePath = _T("test.xml");  
  
pugi::xml_document doc;  
  
doc.load_file(strFilePath.c_str(),pugi::parse_default,pugi::encoding_utf8);  



//pugi::xml_document doc;
//if (!doc.load_file("test.xml")) return -1;
//================================[code_traverse_base_basic====================  
pugi::xml_node IP = doc.child("NetConfig").child("IP");
std::cout << IP.name()<<endl;
for (pugi::xml_node IPele = IP.first_child(); IPele ; IPele  = IPele.next_sibling())
{
for (pugi::xml_attribute attr = IPele.first_attribute(); attr; attr = attr.next_attribute())
{
std::cout << " " << attr.name() << "=" << attr.value()<<" ";//输出IP下面每个标签里的< "属性名=属性值">
}
std::cout << IPele.first_child().value()<<endl;//输出IP地址
}
//=================================


pugi::xml_node URL = doc.child("NetConfig").child("URL");
std::cout << URL.name()<<endl;
for (pugi::xml_node URLele = URL.first_child(); URLele; URLele = URLele.next_sibling())
{
for (pugi::xml_attribute attr = URLele.first_attribute(); attr; attr = attr.next_attribute())
{
std::cout << " " << attr.name() << "=" << attr.value() << " ";
}
std::cout << URLele.first_child().value() << endl;
}
//===========================================
pugi::xml_node ipSeg = doc.child("NetConfig").child("ipSeg");
std::cout << ipSeg.name() << endl;
for (pugi::xml_node ipSegele = ipSeg.first_child(); ipSegele; ipSegele = ipSegele.next_sibling())
{
for (pugi::xml_attribute attr = ipSegele.first_attribute(); attr; attr = attr.next_attribute())
{
std::cout << " " << attr.name() << "=" << attr.value() << " ";
}
//std::cout << ipSegele.first_child().value() << endl;
for (pugi::xml_node ipSegelePlus = ipSegele.first_child(); ipSegelePlus; ipSegelePlus = ipSegelePlus.next_sibling())
{
std::cout << ipSegelePlus.first_child().value() << endl;
}
}


getchar();


}
原创粉丝点击