一、pugixml的简介
pugixml是一个简单方便的XML操作库,pugixml的官网是:http://pugixml.org/我们在上面可以下载得到最新版本的pugixml库。下载后主要使用里面的三个源文件,如下图。
将这三个源文件加人到工程里面,在要使用的地方引入头文件如下:
#include "pugixml.hpp"
using namespace pugi;
编译会报fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?的错误,我们只需要在pugixml.cpp最前面引入stdafx.h头文件即可
#include"stdafx.h"
二、XML树形结构菜单
<?xml version="1.0" encoding="gb2312" ?>
<menu>
<menuitem name="文件(&F)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="新建(&N)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="项目(&P)" type="STRING" cmd="WJ_NEW_XM" accelkey="MOD_CONTROL|MOD_SHIFT|A" checked="true"/>
<menuitem name="网站(&W)" type="STRING" cmd="WJ_NEW_WZ" accelkey="MOD_CONTROL|B" checked="false"/>
<menuitem name="文件(&F)" type="STRING" cmd="WJ_NEW_WJ" accelkey="VK_F8" checked="true"/>
</menuitem>
<menuitem name="打开(&O)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="项目(&P)" type="STRING" cmd="WJ_OPEN_XM" accelkey="MOD_ALT|Q" checked=""/>
<menuitem name="网站(&W)" type="STRING" cmd="WJ_OPEN_WZ" accelkey="" checked=""/>
<menuitem name="文件(&F)" type="STRING" cmd="WJ_OPEN_WJ" accelkey="" checked=""/>
</menuitem>
<menuitem name="添加(&D)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="新建项目(&N)" type="STRING" cmd="WJ_ADD_NEWXM" accelkey="" checked=""/>
<menuitem name="新建网站(&W)" type="STRING" cmd="WJ_ADD_NEWWZ" accelkey="" checked=""/>
<menuitem name="" type="SEPARATOR" cmd="" accelkey="" checked=""/>
<menuitem name="现有项目(&E)" type="STRING" cmd="WJ_ADD_CURXM" accelkey="" checked=""/>
<menuitem name="现有网站(&B)" type="STRING" cmd="WJ_ADD_CURWZ" accelkey="" checked=""/>
</menuitem>
<menuitem name="关闭(&C)" type="STRING" cmd="WJ_CLOSE" accelkey="MOD_SHIFT|C" checked=""/>
</menuitem>
<menuitem name="编辑(&E)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="剪切(&T)" type="STRING" cmd="BJ_CUT" accelkey="MOD_CONTROL|X" checked=""/>
<menuitem name="复制(&C)" type="STRING" cmd="BJ_COPY" accelkey="MOD_CONTROL|C" checked=""/>
<menuitem name="粘贴(&P)" type="STRING" cmd="BJ_PASTE" accelkey="MOD_CONTROL|V" checked=""/>
<menuitem name="删除(&D)" type="STRING" cmd="BJ_DELETE" accelkey="VK_DELETE" checked=""/>
</menuitem>
<menuitem name="视图(&V)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="下一任务(&X)" type="STRING" cmd="ST_NEXT" accelkey="" checked=""/>
<menuitem name="上一任务(&R)" type="STRING" cmd="ST_PRE" accelkey="" checked=""/>
</menuitem>
<menuitem name="项目(&P)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="项目依赖项(&S)" type="STRING" cmd="XM_YLX" accelkey="" checked=""/>
<menuitem name="项目生成顺序(&I)" type="STRING" cmd="XM_SCSX" accelkey="" checked=""/>
</menuitem>
<menuitem name="帮助(&H)" type="POPUP" cmd="" accelkey="" checked="">
<menuitem name="关于(&A)" type="STRING" cmd="BZ_GY" accelkey="" checked=""/>
</menuitem>
</menu>
三、菜单存储树结构
#pragma once
#include<vector>
using namespace std;
//菜单存储树结构
typedef struct menunode
{
//菜单项类型 POPUP STRING SEPARATOR
CString strType;
//菜单项名称
CString strName;
//菜单项命令
CString strCmd;
//菜单项加速键
CString strAccelkey;
//菜单项是否标记
CString strChecked;
//父节点
menunode * ptrParent;
//子节点
vector<menunode *> ptrChildren;
menunode()
{
strType = "";
strName = "";
strCmd = "";
strAccelkey = "";
strChecked = "";
ptrParent = NULL;
ptrChildren.clear();
}
}TS_MENUNODE, *TS_PMENUNODE;
四、读取菜单XML文件
定义int m_nStrNum;用来存储所有STRING类型的菜单项个数
int GetMenuXml(CString strXmlFile, TS_PMENUNODE &tsMenuNode)
{
m_nStrNum = 0;
xml_document doc; //创建一个DOC指针
//xml_parse_result result = doc.load_file(_T(strXmlFile), pugi::parse_default, pugi::encoding_utf8);
xml_parse_result result = doc.load_file(_T(strXmlFile));
xml_node root = doc.child("menu");
if (root)
{
GetMenuChildrenNode(root, tsMenuNode);
}
else
{
AfxMessageBox("获取根结点失败!");
}
return m_nStrNum;
}
bool GetMenuChildrenNode(xml_node &Node, TS_PMENUNODE &tsMenuNode)
{
bool ret = true;
for (xml_node tool = Node.first_child(); tool; tool = tool.next_sibling())
{
TS_PMENUNODE ptrNode = new TS_MENUNODE();
ptrNode->ptrParent = tsMenuNode;
ptrNode->strType = tool.attribute("type").value();
ptrNode->strName = tool.attribute("name").value();
tsMenuNode->ptrChildren.push_back(ptrNode);
if ("POPUP" == ptrNode->strType.MakeUpper())
{
//查找子节点
GetMenuChildrenNode(tool, ptrNode);
}
else if ("SEPARATOR" == ptrNode->strType.MakeUpper())
{
}
else if ("STRING" == ptrNode->strType.MakeUpper())
{
ptrNode->strCmd = tool.attribute("cmd").value();
ptrNode->strAccelkey = tool.attribute("accelkey").value();
ptrNode->strChecked = tool.attribute("checked").value();
m_nStrNum++;
}
}
return ret;
}
到此这一节使用pugixml读取XML树形结构菜单就完成了,下一节我们再来研究菜单项的动态添加、响应、加速键及菜单标记的内容。