如何使用微软的XML解析动态连接库(msxml4.dll)
如果使用微软的XML解析动态连接库(msxml4.dll) |
|
< type="text/javascript">cpro_client='scorpioylj_cpr';cpro_at='text';cpro_161=4;cpro_flush=2;cpro_uap=1;cpro_w=960;cpro_h=90;cpro_template='text_default_960_90';cpro_cbd='#trans';cpro_cbg='#trans';cpro_ctitle='#0066CC';cpro_cdesc='#444444';cpro_curl='#008000';cpro_cflush='#e10900'; < src="zotero://attachment/4729/cp.js" type="text/javascript"> |
< type="text/javascript">cpro_client='scorpioylj_cpr';cpro_at='text';cpro_161=4;cpro_flush=2;cpro_w=760;cpro_h=90;cpro_template='text_default_760_90';cpro_cbd='#FFFFFF';cpro_cbg='#FFFFFF';cpro_ctitle='#0066cc';cpro_cdesc='#444444';cpro_curl='#008000';cpro_cflush='#e10900';cpro_uap=1;cpro_cad=1; < src="zotero://attachment/4729/cp.js" type="text/javascript">
网上购物、在线购物、购物搜索
欢迎光临本站购买图书、影视、音乐、数码、百货,手机等商品。
使用XML解析(msxml4.dll)
c++代码示例以下代码从csdn论坛摘抄。
#include "stdafx.h"
#include <windows.h>
#include <msxml2.h>
#import <msxml4.dll>
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(¤tNode)) && 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"\r\n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
attributeID->Release(); attributeID = NULL;
}
else
{
WriteFile(logFile, L"\r\n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
}
attributes->Release(); attributes = NULL;
}
else
{
WriteFile(logFile, L"\r\n", (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(L"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"\r\n", (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
c++代码示例以下代码从csdn论坛摘抄。
#include "stdafx.h"
#include <windows.h>
#include <msxml2.h>
#import <msxml4.dll>
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(¤tNode)) && 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"\r\n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
attributeID->Release(); attributeID = NULL;
}
else
{
WriteFile(logFile, L"\r\n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
}
attributes->Release(); attributes = NULL;
}
else
{
WriteFile(logFile, L"\r\n", (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(L"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"\r\n", (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