此页面上的内容需要较新版本的 Adobe Flash Player。

获取 Adobe Flash Player

您现在的位置: 智可网 - 程序开发 - 微软开发专区 - Visual C - 正文
VC++对XML文件操作
教程录入:鱼木    责任编辑:yumu 作者:佚名 文章来源:中国it实验室

 <IMG style="WIDTH: 630px; DISPLAY: inline; HEIGHT: 345px" 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文件需要的节点属性值,以及清空文本框。

分享
打赏我
打开支付宝"扫一扫" 打开微信"扫一扫"
客户端
"扫一扫"下载智可网App
意见反馈
VC++对XML文件操作
作者:佚名 来源:中国it实验室

 <IMG style="WIDTH: 630px; DISPLAY: inline; HEIGHT: 345px" 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文件需要的节点属性值,以及清空文本框。