<IMG alt=\ src
void COperateXMLDlg::OnBnClickedGen()
{
//::CoInitialize(NULL); //初始化COM
// TODO: 在此添加控件通知處理程序代碼
UpdateData(TRUE);
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr XMLRoot ;
//創建DOMDocument對象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
if(!SUCCEEDED(hr))
{
MessageBox(_T("無法創建DOMDocument對象,請檢查是否安裝了MS XML Parser 運行庫!"));
return ;
}
//根節點的名稱為Book
//創建元素並添加到文檔中
XMLRoot=pDoc->createElement((_bstr_t)"Book");
//設置屬性
XMLRoot->setAttribute("id",(const _variant_t)m_strId);
pDoc->appendChild(XMLRoot);
MSXML2::IXMLDOMElementPtr pNode;
//添加"author"元素
pNode=pDoc->createElement((_bstr_t)"Author");
pNode->Puttext((_bstr_t)m_strAuthor);
XMLRoot->appendChild(pNode);
//添加"Title"元素
pNode=pDoc->createElement((_bstr_t)"Title");
pNode->Puttext((_bstr_t)m_strTitle);
XMLRoot->appendChild(pNode);
//保存到文件
//如果不存在就建立,存在就覆蓋
pDoc->save(_T("d://he.XML"));
//::CoUninitialize();
}
void COperateXMLDlg::OnBnClickedGet()
{
// TODO: 在此添加控件通知處理程序代碼
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr;
hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
if(FAILED(hr))
{
MessageBox(_T("無法創建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(count,& pAttrItem);
//pAttrMap->get_item(0,& pAttrItem); //同上,驗證count值
//取得節點的值
pAttrItem->get_nodeTypedValue(&variantvalue);
m_strId=(char *)(_bstr_t)variantvalue;
/* 測試
CString s;
s.Format(_T("%d"),count);
m_strAuthor=(char *)(_bstr_t)s;
*/
//方法二
//MSXML2::IXMLDOMElementPtr pRootNode = pDoc->documentElement;
//查找Book節點
MSXML2::IXMLDOMNodePtr pBookNode = pDoc->selectSingleNode(_T("Book"));
//CString s;
//long size = pBookNode->length;
MSXML2::IXMLDOMNodePtr pAuthorNode = pBookNode->selectSingleNode(_T("Author"));
MSXML2::IXMLDOMNodePtr pTitleNode = pBookNode->selectSingleNode(_T("Title"));
//s.Format(_T("%sauthor: %s\ttitle: %s\r\n"),s,(LPCTSTR)pAuthorNode->text,(LPCTSTR)pTitleNode->text);
MSXML2::DOMNodeType nodeType;
//得到節點類型
pBookNode->get_nodeType(&nodeType);
MSXML2::IXMLDOMNamedNodeMaPPTr pAttrMap=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
pBookNode->get_attributes(&pAttrMap); //獲取節點所有屬性
long count;
count=pAttrMap->get_length(&count);
pAttrMap->get_item(count,& pAttrItem); //獲得第count個item值
_variant_t variantvalue;
pAttrItem->get_nodeTypedValue(&variantvalue);
//m_strId = (char *)(_bstr_t)variantvalue;
//獲取book的id屬性的方法三
//MSXML2::IXMLDOMElementPtr pRootNode = pDoc->documentElement;//此方法獲得XML跟節點
//variantvalue = pRootNode->getAttribute(_bstr_t("id"));
MSXML2::IXMLDOMElementPtr pBNode = pBookNode; //獲取選中節點的元素節點地址
variantvalue = pBNode->getAttribute(_bstr_t("id"));
m_strId = (char *)(_bstr_t)variantvalue;
m_strAuthor = (LPCTSTR)pAuthorNode->text;
m_strTitle = (LPCTSTR)pTitleNode->text;
UpdateData(FALSE);
}
void COperateXMLDlg::OnBnClickedClr()
{
// TODO: 在此添加控件通知處理程序代碼
//UpdateData(TRUE);
m_strId = (LPCTSTR)("");
m_strAuthor = (LPCTSTR)("");
m_strTitle = (LPCTSTR)("");
UpdateData(FALSE);
}
以上三個按鈕分別表示將數據寫入XML文件,讀取XML文件需要的節點屬性值,以及清空文本框。