简介:
RapidXml 试图成为最快的 XML DOM 解析工具包,同时保证解析结果的可用性、可移植性以及与 W3C 标准的兼容性。RapidXml 使用 C++ 编写,因此在操作同一数据时,其解析速度接近于 strlen() 函数。
整个解析工具包包含在一个头文件中,所以使用时不用编译也不用连接。要想使用 RapidXml 要包含 rapidxml.hpp ,rapidxml_utils.hpp,当然如果要用附加功能(如打印函数),你可以包含 rapidxml_print.hpp 文件。
RapidXml 为采用C++语言操作XML提供了机遇,同时结合XMPP协议也开启了诸如
Wt、CxServer等基于C++的网络应用在即时通讯领域的更宽广的发展空间。
1 . 打开文件:
rapidxml::file<> fdoc(filepath) ;
2 . 获取跟节点
//分析plist文件字符串,要求已'\0'结束
rapidxml::xml_document<> doc ;
doc.parse<0>(fdoc.data()) ;
//获取根节点
rapidxml::xml_node<> *root = doc.first_node() ;
3 . 遍历所有节点(循环中间用递归调用实现所有节点的遍历)
for( rapidxml::xml_node *node = node->first_node() ; node != NULL ; node = node->next_sibling() )
for( rapidxml::xml_attribute<> *attr = node->first_attribute() ; attr != NULL ; attr = attr->next_attribute() )
/*
* 文件说明:rapidxml库解析plist文件头文件
* 作 者:黄平
*
*/
#ifndef _RAPIDPLIST_RAPIDXML_PLIST_H_
#define _RAPIDPLIST_RAPIDXML_PLIST_H_
#include <iostream>
#include <string>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"
using namespace std ;
namespace rapidplist
{
class rapidxml_plist_scanner
{
public:
//构造函数
rapidxml_plist_scanner()
{
space_num_ = 0 ;
}
//解析文件内容
void read_file(const char *path) ;
//输出空格
void print_space() ;
void print_space(ofstream &out) ;
//遍历所有节点
void traversal_node(rapidxml::xml_node<> *node) ;
//遍历所有属性
void traversal_attribute(rapidxml::xml_node<> *node) ;
private:
ofstream write_file_ ; //写文件指针
int space_num_ ; //记录空格个数,控制输出格式
};
}
#endif //_RAPIDPLIST_RAPIDXML_PLIST_H_
/*
* 文件说明:rapidxml库解析plist文件源文件
* 作 者:黄平
*
*/
#include "rapidxml_plist.h"
using namespace rapidplist ;
//输出空格控制格式
void rapidxml_plist_scanner::print_space()
{
for(int i=0 ; i < space_num_ ; ++i)
cout << ' ' ;
}
void rapidxml_plist_scanner::print_space(ofstream &out)
{
for(int i=0 ; i < space_num_ ; ++i)
out << ' ' ;
}
//遍历所有节点
void rapidxml_plist_scanner::traversal_node(rapidxml::xml_node<> *node)
{
for( node = node->first_node() ; node != NULL ; node = node->next_sibling() )
{
string name = node->name() ; //获取节点的名字
if( name != "") //节点存在时才进行操作
{
//单独处理<true/> <false/>标签的输出格式
if( name == "true" || name == "false" )
{
cout << name << endl ;
write_file_ << name << endl ; //写入文件
}
else
{
cout << endl ;
write_file_ << endl ; //写入文件
print_space() ; //输出空格,控制格式
print_space(write_file_) ; //控制文件格式
//输出节点的值和属性
cout << "< " << name ;
write_file_ << "< " << name ; //写入文件
traversal_attribute(node); //获取节点的属性
cout << " >" << endl ;
write_file_ << " >" << endl ; //写入文件
//控制输出格式
if( name == "dict" || name == "array")
{
print_space();
print_space(write_file_) ; //控制文件格式
if( name == "dict" )
{
cout << '{' << endl ;
write_file_ << '{' ; //写入文件
}
else
{
cout << '[' << endl ;
write_file_ << '[' ; //写入文件
}
space_num_ += 3 ;
}
}
//递归遍历子节点
traversal_node(node) ;
string value = node->value() ; //获取节点的值
//控制输出格式
if( name == "dict" || name == "array")
{
print_space() ;
print_space(write_file_) ; //控制文件格式
cout << value << endl ;
write_file_ << value << endl ; //写入文件
space_num_ -= 3 ;
print_space();
print_space(write_file_) ; //控制文件格式
if( name == "dict" )
{
cout << '}' << endl ;
write_file_ << '}' << endl ; //写入文件
}
else
{
cout << ']' << endl ;
write_file_ << ']' << endl ; //写入文件
}
}
else if( name == "key" )
{
print_space() ;
print_space(write_file_) ; //控制文件格式
cout << value << " = " ;
write_file_ << value << " = " ; //写入文件
}
else
{
print_space() ;
print_space(write_file_) ; //控制文件格式
cout << value << endl ;
write_file_ << value << endl ; //写入文件
}
}
}
}
//遍历所有属性
void rapidxml_plist_scanner::traversal_attribute(rapidxml::xml_node<> *node)
{
for( rapidxml::xml_attribute<> *attr = node->first_attribute() ; attr != NULL ; attr = attr->next_attribute() )
{
string value = attr->value() ; //获取属性值
cout << ' ' << value ; //输出属性值
write_file_ << ' ' << value ; //写入文件
}
}
//解析文件内容
void rapidxml_plist_scanner::read_file(const char *path)
{
//打开文件失败
rapidxml::file<> fdoc(path) ; //读文件指针
//cout << fdoc.data() << endl ; //输出整个文档
//分析plist文件字符串,要求已'\0'结束
rapidxml::xml_document<> doc ;
doc.parse<0>(fdoc.data()) ;
//获取根节点
rapidxml::xml_node<> *root = doc.first_node() ;
//cout << root->name() << endl ; //输出plist
write_file_.open("rapidxml.txt",ios::app) ;
if( !write_file_ )
{
cerr << "Failed to open the write file !" << endl ;
return ;
}
//遍历所有的节点
traversal_node(root) ;
//关闭文件
write_file_.close() ;
}
/*
* 文件说明:rapidxml库解析plist文件入口,主函数
* 作 者:黄平
*
*/
#include "rapidxml_plist.h"
int main()
{
string path;
while(1)
{
cin >> path ;
rapidplist::rapidxml_plist_scanner obj ;
obj.read_file(path.c_str());
}
return 0;
}