淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

MSXML6 SDK解析中文XML文件

上一篇 / 下一篇  2008-08-10 01:36:58 / 个人分类:loadrunner性能测试经验

1.1      下载msxml6_SDK.msi安装

 

http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en

 

默认安装在C:\Program Files\MSXML 6.0\

 

1.2      vc6上建立编译环境

Preprocessor编译加入Additional include directorie加入C:\Program Files\MSXML 6.0\inc

 

LINK module加入: msxml6.lib

 

Link Additional library path加入:C:\Program Files\MSXML 6.0\lib

1.3      解决vc6 link错误问题

 

msxml6.lib(msxml6_i.obj) : fatal error LNK1103: debugging information corrupt; recompile module

 

参见

http://www.armleg.com/forum/viewtopic.php?p=388&sid=414fd259dcf02a90f150708296924178&mforum=diodiaforum

 

All I get from Google is that VS6 doesn't work with platform SDK's later than february 2003.

 

必须采用release版本编译、链接才成功。(build->set active project configuration->win32 release)

 

 

1.4      汉语问题

 

      //解决汉语问题

      setlocale(LC_ALL,"chinese-simplified");

 

1.5      源代码

 

#include <objbase.h>

#include <msxml6.h>

#include <stdio.h>

#include <windows.h>

#include <stdarg.h>

#include <locale.h>

#include <AtlBase.h>

 

#import <msxml6.dll> raw_interfaces_only

 

// Macro that calls a COM method returning HRESULT value:

#define HRCALL(a, errmsg) \

do { \

   hr = (a); \

   if (FAILED(hr)) { \

       dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \

               __FILE__, __LINE__, errmsg, hr, #a ); \

       goto clean; \

   } \

} while (0)

 

// Helper function that put output in stdout and debug window

// in Visual Studio:

void dprintf( char * format, ...)

{

   static char buf[1024];

   va_list args;

   va_start( args, format );

      sprintf(buf, format, args);

   vsprintf(buf, format, args );

   va_end( args);

   OutputDebugStringA( buf);

   printf("%s", buf);

}

 

// Helper function to create a DOM instance:

IXMLDOMDocument3 * DomFromCOM()

{

  HRESULT hr;

  IXMLDOMDocument3 *pxmldoc = NULL;

 

  HRCALL( CoCreateInstance(CLSID_DOMDocument60,

                 NULL,

                 CLSCTX_INPROC_SERVER,

                 //__uuidof(IXMLDOMDocument),

                            IID_IXMLDOMDocument3,

                 (void**)&pxmldoc),

                 "Create a new DOMDocument");

 

   HRCALL( pxmldoc->put_async(VARIANT_FALSE),

           "should never fail");

   HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),

           "should never fail");

   HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),

           "should never fail");

 

   return pxmldoc;

clean:

   if (pxmldoc)

   {

       pxmldoc->Release();

   }

   return NULL;

}

 

int Get_nodeValue(IXMLDOMElement * pRoot , char * tagName,char *ret_text)

{

      USES_CONVERSION;

   IXMLDOMNodeList * pNodeList = NULL;

      IXMLDOMNode * pNode = NULL;

      HRESULT hr;

      BSTR bstr = NULL;

 

      hr=pRoot->getElementsByTagName(_bstr_t(tagName),&pNodeList) ;

      if (FAILED(hr))

             return -1;

 

      //取第一个满足条件的

      hr= pNodeList->get_item(0,&pNode);

      if (FAILED(hr))

             return -1;

      

      hr=pNode->get_text(&bstr);

      if (FAILED(hr))

             return -1;

      //dprintf("v=%s\n",W2A(bstr));

      sprintf(ret_text,W2A(bstr));

      return 0;

}

 

int main(int argc, char* argv[])

{

 /*

 HRESULT hr;

 IXMLDOMDocument3 *pXMLDoc = NULL;

 CoInitialize(NULL);

 hr = CoCreateInstance(CLSID_DOMDocument60,

                    NULL,

                    CLSCTX_INPROC_SERVER,

                    IID_IXMLDOMDocument3,

                    (void**)&pXMLDoc);

  if (FAILED(hr))

  {

     printf("Error code: %x\n", hr);

  }

*/

      USES_CONVERSION;

   IXMLDOMDocument3 *pXMLDom=NULL;

   IXMLDOMParseError *pXMLErr=NULL;

   BSTR bstr = NULL;

   VARIANT_BOOL status;

   VARIANT var;

   HRESULT hr;

      IXMLDOMElement * pRoot = NULL;

      VARIANT_BOOL isHasChild=VARIANT_FALSE;

      long listLen=0;

      long i=0;

      char ret_scrīpttext[48]={0};

      char ret_steptime[48]={0};

      char sz_xmlFile[]="lr.xml";

      

   CoInitialize(NULL);

      //解决汉语编码问题

      setlocale(LC_ALL,"chinese-simplified");

 

   pXMLDom = DomFromCOM();

   if (!pXMLDom) 

             goto clean;

      

   VariantInit(&var);

   V_BSTR(&var) = SysAllocString(_bstr_t(sz_xmlFile));

   V_VT(&var) = VT_BSTR;

   HRCALL(pXMLDom->load(var, &status), "");

 

   if (status!=VARIANT_TRUE) {

       HRCALL(pXMLDom->get_parseError(&pXMLErr),"");

       HRCALL(pXMLErr->get_reason(&bstr),"");

       dprintf("Failed to load DOM from stocks.xml. %S\n",

                   bstr);

       goto clean;

      

 

   }

   HRCALL(pXMLDom->get_xml(&bstr), "");

 

   dprintf("XML DOM loaded from stocks.xml:\n%S\n",bstr);

 

      HRCALL( pXMLDom->get_documentElement(&pRoot) ,"get_documentElement");

  

      Get_nodeValue(pRoot,"scrīpt",ret_scrīpttext);

      Get_nodeValue(pRoot,"steptime",ret_steptime);

      

      printf("%s %d",ret_scrīpttext,atoi(ret_steptime));

 /*

   HRCALL(pNode->get_nodeValue(&value),"get_nodeValue");

      USES_CONVERSION;  

   dprintf("v=%s\n",OLE2A(value.bstrVal));

      SysFreeString(bstr);

*/

 

clean:

   if (bstr) SysFreeString(bstr);

   if (&var) VariantClear(&var);

   if (pXMLErr) pXMLErr->Release();

   if (pXMLDom) pXMLDom->Release();

 

   CoUninitialize();

   return 0;

}

 

Lr.Xml文件如下(notepad保存为ascii格式)

<?xml version="1.0" encoding="GB2312"?>

 

<root>

   <scrīpt type="string">d:\工程\1.lrr</scrīpt>

   <steptime>30</steptime>

</root>


TAG: MSXML setlocale 汉字 xml get_documentElement SysAllocString BSTR

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar