xmldlg.cpp
来自「Userful I think you need now please down」· C++ 代码 · 共 727 行 · 第 1/2 页
CPP
727 行
// XMLDlg.cpp : implementation file
//
#include "stdafx.h"
#include "XML.h"
#include "XMLDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//在DOM 接口规范中,有四个基本的接口:Document,Node,NodeList以及NamedNodeMap
//Document接口是对文档进行操作的入口,它是从Node接口继承过来的
MSXML2::IXMLDOMDocumentPtr pDoc;
//Node接口是其他大多数接口的父类,象Documet,Element,Attribute,Text,Comment等接口都是从Node接口继承过来的
MSXML2::IXMLDOMElementPtr xmlRoot ;
MSXML2::IXMLDOMElementPtr pNode;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int i;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXMLDlg dialog
CXMLDlg::CXMLDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXMLDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CXMLDlg)
m_english = _T("");
m_maths = _T("");
m_name = _T("");
m_no = _T("");
m_profession = _T("");
m_number = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CXMLDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXMLDlg)
DDX_Control(pDX, IDC_LIST, m_list);
DDX_Text(pDX, IDC_EDIT_english, m_english);
DDX_Text(pDX, IDC_EDIT_maths, m_maths);
DDX_Text(pDX, IDC_EDIT_name, m_name);
DDX_Text(pDX, IDC_EDIT_no, m_no);
DDX_Text(pDX, IDC_EDIT_profession, m_profession);
DDX_Text(pDX, IDC_EDIT_number, m_number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CXMLDlg, CDialog)
//{{AFX_MSG_MAP(CXMLDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_write, OnBUTTONwrite)
ON_BN_CLICKED(IDC_BUTTON_read, OnBUTTONread)
ON_BN_CLICKED(IDC_BUTTON_clear, OnBUTTONclear)
ON_BN_CLICKED(IDC_BUTTON_import, OnBUTTONimport)
ON_BN_CLICKED(IDC_BUTTON_out, OnBUTTONout)
ON_BN_CLICKED(IDC_BUTTON_explain, OnBUTTONexplain)
ON_BN_CLICKED(IDC_BUTTON_allsql, OnBUTTONallsql)
ON_BN_CLICKED(IDC_BUTTON_clear_sql, OnBUTTONclearsql)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXMLDlg message handlers
BOOL CXMLDlg::OnInitDialog()
{
CDialog::OnInitDialog();
i=0;
// Add "About..." menu item to system menu.
m_list . InsertColumn ( 0, "英语", LVCFMT_LEFT, 120);
m_list . InsertColumn ( 1, "数学", LVCFMT_LEFT, 120);
m_list . InsertColumn ( 2, "专业课", LVCFMT_LEFT, 100);
m_list . InsertColumn ( 3, "姓名", LVCFMT_CENTER, 80 );
m_list . InsertColumn ( 4, "学号", LVCFMT_CENTER, 80 );
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser运行库!");
return true;
}
//根节点的名称为Book
//创建元素并添加到文档中
/*--------------------------createElement--------------------------
Description
Creates an instance of the element object for the specified tag. Only new IMG and
OPTION elements can be created. Before they can be used, new objects must be explicitly
added to their respective collections
Syntax
element = object.createElement(tag)
Parameter Description
tag (String) Tag specifier.
Return Value
Returns an element object.
Applies To
document
*/
xmlRoot=pDoc->createElement((_bstr_t)"Book");//创建名称为book的element
pDoc->appendChild(xmlRoot);//pDoc为父元素,xmlRoot为要附加到pDoc之下的结点
return TRUE; // return TRUE unless you set the focus to a control
}
void CXMLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CXMLDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CXMLDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CXMLDlg::OnBUTTONwrite()
{
// TODO: Add your control notification handler code here
/*
DOM是Document Object Model(文档对象模型)的简称,是对XML文档进行应用开发、编程的应用程序
接口(API)。作为W3C公布的一种跨平台、与语言无关的接口规范,DOM提供了在不同环境和应用中的
标准程序接口,可以用任何语言实现。
DOM采用对象模型和一系列的接口来描述XML文档的内容和结构,即利用对象把文档模型化。这种
对象模型实现的基本功能包括:
● 描述文档表示和操作的接口;
● 接口的属性和方法;
● 接口之间的关系以及互操作。
在DOM中主要有以下三个对象:
● XML文档对象
XML文档既是一种对象,同时又代表整个XML文档。它由根元素和子元素组成。
● XML节点对象
XML节点对象代表的是XML文档内部的节点,如元素、注释、名字空间等。
● XML节点列表
XML文档模块列表代表了节点的集合。
利用DOM,开发人员可以动态地创建XML文档,遍历结构,添加、修改、删除内容等。其面向对象的
特性,使人们在处理XML解析相关的事务时节省大量的精力,是一种符合代码重用思想的强有力编程工具。
*/
/*---------------------------------MSXML-------------------------------------------------
从理论上说,根据XML的格式定义,我们可以自己编写一个XML的语法分析器,但实际上微软已经给
我们提供了一个XML语法解析器,即一个叫做 MSXML.DLL的动态链接库,实际上它是一个COM
(Component Object Model)对象库,里面封装了进行XML解析时所需要的所 有对象。因为COM是一种
以二进制格式出现的和语言无关的可重用对象,所以你可以用任何语言(比如VB,VC,DELPHI,
C++ Builder甚至 是脚本语言等等)对它进行调用,在你的应用中实现对XML文档的解析。
MSXML.DLL所包括的主要COM接口有:
1. IXMLDOMDocument(Document接口)
DOMDocument 对象是XML DOM的基础,你可以利用它所暴露的属性和方法来浏览、查询和修改
XML文档的内容和结构。DOMDocument表示了树的顶层节点,它实现了DOM文档的所有的基本方法,
并且提供了额外的成员函数来支持XSL和XSLT。它创建了一个文档对象,所有其他的对象都可以从这个
文档对象中得到 和创建。
2. IXMLDOMNode(Node接口)
IXMLDOMNode是文档对象模型(DOM)中的基本对象,元素、属性、注释、过程指令和其他的文档组件
都可以认为是IXMLDOMNode。事实上,DOMDocument对象本身也是一个IXMLDOMNode对象。
3. IXMLDOMNodeList
IXMLDOMNodeList实际上是一个节点(Node)对象的集合,节点的增加、删除和变化都可以在集合中
立刻反映出来,可以通过"for.循环 "结构来遍历所有的节点。
4. IXMLDOMParseError
IXMLDOMParseError接口用来返回在解析过程中所出现的详细的信息,包括错误号、行号、字符位置
和文本描述。
在具体应用时可以用DOMDocument的Load方法来装载XML文档,用IXMLDOMNode 的selectNodes
(查询的结果有多个, 得到存放搜索结果的链表)或selectSingleNode(查询的结果有一个,在有多个
的情况下返回找到的第一个节点)方法进行查询,用 createNode和appendChild方法来创建节点和追加
节点,用IXMLDOMElement的setAttribute和 getAttribute方法来设置和获得节点的属性。
*/
UpdateData(true);
if(i==m_number)
{
AfxMessageBox("已经完成了指定输入数据的工作,请进行其他操作!");
return;
}
//添加元素
pNode=pDoc->createElement((_bstr_t)"Student"); //创建
pNode->setAttribute("s_name",(const char *)m_name);
pNode->setAttribute("s_number",(const char *)m_no);
xmlRoot->appendChild(pNode); //连接
//继续添加子结点
MSXML2::IXMLDOMElementPtr wopapapa;
wopapapa=pDoc->createElement((_bstr_t)"英语");
wopapapa->Puttext((const char *)m_english);
pNode->appendChild(wopapapa);
wopapapa=pDoc->createElement((_bstr_t)"数学");
wopapapa->Puttext((const char *)m_maths);
pNode->appendChild(wopapapa);
wopapapa=pDoc->createElement((_bstr_t)"专业课");
wopapapa->Puttext((const char *)m_profession);
pNode->appendChild(wopapapa);
++i;
if(i==m_number)
{
pDoc->save("wopapapa.xml");
AfxMessageBox("~~已经添加完毕,可以进行其他的操作~~");
}
m_name.Empty();
m_no.Empty();
m_maths.Empty();
m_english.Empty();
m_profession.Empty();
UpdateData(false);
}
void CXMLDlg::OnBUTTONread()
{
//加载文件
OnBUTTONclear();
MSXML2::IXMLDOMNodePtr psNode;
MSXML2::IXMLDOMNodePtr pmethodAttrItem;
MSXML2::IXMLDOMNodePtr pAttrItem;
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap=NULL;
MSXML2::IXMLDOMNamedNodeMapPtr methodpAttrs=NULL;
_variant_t variantValue;
CString strName;
long nCount;
MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; //解析student
MSXML2::IXMLDOMNodeListPtr pNodeList1 = NULL;//解析英语
MSXML2::IXMLDOMNodeListPtr pNodeList2 = NULL;//解析数学
MSXML2::IXMLDOMNodeListPtr pNodeList3 = NULL;//解析专业课
pDoc->load("wopapapa.xml");
pNodeList=pDoc->getElementsByTagName((_bstr_t)"Student");
int num=pNodeList->Getlength();
CString tt;
CString cs_english,cs_maths,cs_profession;
for(int i=0;i<num;i++)
{
pNodeList1=pDoc->getElementsByTagName((_bstr_t)"英语");
pNodeList2=pDoc->getElementsByTagName((_bstr_t)"数学");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?