VC++对XML文件操作

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Jeremy_W/article/details/23763079
 
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文件需要的节点属性值,以及清空文本框。

展开阅读全文

没有更多推荐了,返回首页