使用msxml4.dll解析xml

使用msxml4.dll解析xml

c++代码示例
#include <windows.h>
#include <msxml2.h>
#import <msxml4.dll>
#include <tchar.h>


HANDLE logFile = NULL;
#define INDENT 4

#define TESTHR(hr) /
{ /
 if(FAILED(hr)) goto fail; /
}


void PrintChild(IXMLDOMNodeList* nodeList, int level)
{
 if(nodeList == NULL)
  return;
   
 IXMLDOMNode* currentNode = NULL;
 IXMLDOMNodeList* childNodes = NULL;
 IXMLDOMNamedNodeMap* attributes = NULL;
 IXMLDOMNode* attributeID = NULL;
 
 while(!FAILED(nodeList->nextNode(&currentNode)) && currentNode != NULL)
 {
  BSTR nodeName;
  TESTHR(currentNode->get_nodeName(&nodeName));
  DWORD dwBytesWritten;
  for(int i=0; i < level * INDENT; i++)
   WriteFile(logFile, L" ", (DWORD)(sizeof(WCHAR)), &dwBytesWritten, NULL);
  
  //WCHAR msg[MAX_SIZE];
  //wsprintf(msg, L"%s ", nodeName);
  WriteFile(logFile, nodeName, (DWORD)(wcslen(nodeName)*sizeof(WCHAR)), &dwBytesWritten, NULL);
  TESTHR(currentNode->get_attributes(&attributes));
  
  if(attributes!=NULL)
  {
   _bstr_t bstrAttributeName = "id";
   BSTR idVal;
   TESTHR(attributes->getNamedItem(bstrAttributeName, &attributeID));
   
   if(attributeID != NULL)
   {
    TESTHR(attributeID->get_text(&idVal));
    WriteFile(logFile, L" ", (DWORD)(sizeof(WCHAR)), &dwBytesWritten, NULL);
    WriteFile(logFile, idVal, (DWORD)(wcslen(idVal)*sizeof(WCHAR)), &dwBytesWritten, NULL);
    WriteFile(logFile, L"rn", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
    attributeID->Release(); attributeID = NULL;
   }
   else
   {
    WriteFile(logFile, L"rn", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
   }
   
   attributes->Release(); attributes = NULL;
  }
  else
  {
   WriteFile(logFile, L"rn", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
  }
  
  TESTHR(currentNode->get_childNodes(&childNodes));
  PrintChild(childNodes, level+1);
  currentNode=NULL;
 }
   
fail:
 if(childNodes!=NULL)
  childNodes->Release();
 
 if(attributeID!=NULL)
  attributeID->Release();
 
 if(attributes!=NULL)
  attributes->Release();
 
 if(currentNode != NULL)
  currentNode->Release();
}

int _tmain(int argc, _TCHAR* argv[])
{
 IXMLDOMDocumentPtr xmlFile = NULL;
 IXMLDOMElement* xmlRoot = NULL;
 _variant_t varXml(L"C:demo1.xml");

    logFile = CreateFile("log.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
 if(logFile == INVALID_HANDLE_VALUE)
  goto fail;
 
 TESTHR(CoInitialize(NULL));
 TESTHR(xmlFile.CreateInstance("Msxml2.DOMDocument.4.0"));
 
 VARIANT_BOOL varOut;
 TESTHR(xmlFile->load(varXml, &varOut));
 TESTHR(xmlFile->get_documentElement(&xmlRoot));
 
 BSTR rootName;
 DWORD dwBytesWritten;
 TESTHR(xmlRoot->get_nodeName(&rootName));
 
 WriteFile(logFile, rootName, (DWORD)(wcslen(rootName)*sizeof(WCHAR)), &dwBytesWritten, NULL);
 WriteFile(logFile, L"rn", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
 
 IXMLDOMNodeList* xmlChildNodes = NULL;
 TESTHR(xmlRoot->get_childNodes(&xmlChildNodes));
 
 PrintChild(xmlChildNodes, 2);
   
fail:
 if(logFile != INVALID_HANDLE_VALUE)
  CloseHandle(logFile);
 
 if(xmlChildNodes!=NULL)
  xmlChildNodes->Release();
 
 if(xmlRoot!=NULL)
  xmlRoot->Release();
 
 return 0;
}

 

安装部署
将使用msxml4.dll的程序打包的时候,要在安装部署项目中添加合并模块msxm4sys32.msm,以下介绍添加步骤。

添加安装和部署项目
要将当前项目打包成安装程序的时候,可以用.net提供的安装程序制作。在当前项目中,打开菜单:文件à添加项目à新建项目,将弹出“添加新项目”对话框,在“项目类型”中选择“安装和部署项目”,“模板”选择“安装项目”,填写“名称”和“目录”,在解决方案资源管理器中就会生成一个安装部署项目。右击该安装项目,在弹出的菜单中选择“视图”à“文件系统”,在文件系统中可以添加任何想要包括在安装程序中的文件。


安装msxm4sys32.msm
如果在C:/Program Files/Common Files/Merge Modules已经有msxm4sys32.msm这个文件,就可以跳过这一步。

下载并安装msxml4msms.exe,msxml4msms.exe可以在以下地址中下载,该文件中包含了msxml4sxs32.msm 和 msxm4sys32.msm。

http://download.microsoft.com/download/9/6/5/9657c01e-107f-409c-baac-7d249561629c/msxml4msms.exe

 

添加合并模块
右击安装项目,在弹出的菜单中选择“添加”à“合并模块”,在选择文件的对话框中选择“msxm4sys32.msm”。


参考资料

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetframedepguid.asp?frame=true
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值