来自http://blog.csdn.net/goingup/archive/2006/03/07/618314.aspx
1、目标文档:
<book id="bk101">
<author>lizlex</author>
<title>XML Developer's Guide</title>
</book>
2、步骤:
(1)在StdAfx.h中引入动态链接库 MSXML.DLL(C:\windows\system32\msxml4.dll)
#import <msxml4.dll>
(2)界面设计:
分别放入三个Text,用于输入数据,与显示文档内容用,并添加关联的成员变量m_strId,m_strAuthor, m_strTitle;并添加确定按钮:
(3)产生文档的程序片断
void CXmlparseDlg::OnButtonGenerate()
{
UpdateData();
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
//创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
return ;
}
//根节点的名称为Book
//创建元素并添加到文档中
xmlRoot=pDoc->createElement((_bstr_t)"Book");
//设置属性
xmlRoot->setAttribute("id",(const char *)m_strId);
pDoc->appendChild(xmlRoot);
MSXML2::IXMLDOMElementPtr pNode;
//添加“author”元素
pNode=pDoc->createElement((_bstr_t)"Author");
pNode->Puttext((_bstr_t)(const char *)m_strAuthor);
xmlRoot->appendChild(pNode);
//添加“Title”元素
pNode=pDoc->createElement("Title");
pNode->Puttext((const char *)m_strTitle);
xmlRoot->appendChild(pNode);
//保存到文件
//如果不存在就建立,存在就覆盖
pDoc->save("d:\\he.xml");
}
(4)读取XML文档的程序片断
void CXmlparseDlg::OnButtonLoad()
{
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr;
hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(FAILED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
return ;
}
//加载文件
pDoc->load("d:\\he.xml");
MSXML2::IXMLDOMNodePtr pNode;
//在树中查找名为Book的节点,"//"表示在任意一层查找
pNode=pDoc->selectSingleNode("//Book");
MSXML2::DOMNodeType nodeType;
//得到节点类型
pNode->get_nodeType(&nodeType);
//节点名称
CString strName;
strName=(char *)pNode->GetnodeName();
//节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
_variant_t variantvalue;
pNode->get_attributes(&pAttrMap);
long count;
count=pAttrMap->get_length(&count);
pAttrMap->get_item(0,&pAttrItem);
//取得节点的值
pAttrItem->get_nodeTypedvalue(&variantvalue);
m_strId=(char *)(_bstr_t)variantvalue;
UpdateData(FALSE);
}
<book id="bk101">
<author>lizlex</author>
<title>XML Developer's Guide</title>
</book>
2、步骤:
(1)在StdAfx.h中引入动态链接库 MSXML.DLL(C:\windows\system32\msxml4.dll)
#import <msxml4.dll>
(2)界面设计:
分别放入三个Text,用于输入数据,与显示文档内容用,并添加关联的成员变量m_strId,m_strAuthor, m_strTitle;并添加确定按钮:
(3)产生文档的程序片断
void CXmlparseDlg::OnButtonGenerate()
{
UpdateData();
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
//创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
return ;
}
//根节点的名称为Book
//创建元素并添加到文档中
xmlRoot=pDoc->createElement((_bstr_t)"Book");
//设置属性
xmlRoot->setAttribute("id",(const char *)m_strId);
pDoc->appendChild(xmlRoot);
MSXML2::IXMLDOMElementPtr pNode;
//添加“author”元素
pNode=pDoc->createElement((_bstr_t)"Author");
pNode->Puttext((_bstr_t)(const char *)m_strAuthor);
xmlRoot->appendChild(pNode);
//添加“Title”元素
pNode=pDoc->createElement("Title");
pNode->Puttext((const char *)m_strTitle);
xmlRoot->appendChild(pNode);
//保存到文件
//如果不存在就建立,存在就覆盖
pDoc->save("d:\\he.xml");
}
(4)读取XML文档的程序片断
void CXmlparseDlg::OnButtonLoad()
{
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr;
hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(FAILED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
return ;
}
//加载文件
pDoc->load("d:\\he.xml");
MSXML2::IXMLDOMNodePtr pNode;
//在树中查找名为Book的节点,"//"表示在任意一层查找
pNode=pDoc->selectSingleNode("//Book");
MSXML2::DOMNodeType nodeType;
//得到节点类型
pNode->get_nodeType(&nodeType);
//节点名称
CString strName;
strName=(char *)pNode->GetnodeName();
//节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
_variant_t variantvalue;
pNode->get_attributes(&pAttrMap);
long count;
count=pAttrMap->get_length(&count);
pAttrMap->get_item(0,&pAttrItem);
//取得节点的值
pAttrItem->get_nodeTypedvalue(&variantvalue);
m_strId=(char *)(_bstr_t)variantvalue;
UpdateData(FALSE);
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=618314