This is not a question because I have already found a solution. It took me a long time, so I want to explain it here.
Msxml is based on COM, so in C ++ it is not so simple, even if you have useful classes for solving memory allocation problems. But writing a new XML parser would be much more difficult, so I wanted to use msxml.
Problem:
I managed to find enough examples on the Internet to use msxml with CComPtr (a smart pointer to avoid having to manually call Release () for each IXMLDOMNode file), CComBSTR (to convert C ++ strings to COM for strings) and CComVariant . These 3 useful classes are ATL classes and need #include <atlbase.h> .
Problem: Visual Studio 2008 Express (free version) does not include ATL.
Decision:
Use comutil.h and comdef.h , which include some simple helper classes:
_bstr_t replaces more or less CComBSTR_variant_t replaces more or less CComVariant_com_ptr_t indirectly replaced by CComPtr with _COM_SMARTPTR_TYPEDEF
A small example:
#include <msxml.h> #include <comdef.h> #include <comutil.h> // Define some smart pointers for MSXML _COM_SMARTPTR_TYPEDEF(IXMLDOMDocument, __uuidof(IXMLDOMDocument)); // IXMLDOMDocumentPtr _COM_SMARTPTR_TYPEDEF(IXMLDOMElement, __uuidof(IXMLDOMElement)); // IXMLDOMElementPtr _COM_SMARTPTR_TYPEDEF(IXMLDOMNodeList, __uuidof(IXMLDOMNodeList)); // IXMLDOMNodeListPtr _COM_SMARTPTR_TYPEDEF(IXMLDOMNamedNodeMap, __uuidof(IXMLDOMNamedNodeMap)); // IXMLDOMNamedNodeMapPtr _COM_SMARTPTR_TYPEDEF(IXMLDOMNode, __uuidof(IXMLDOMNode)); // IXMLDOMNodePtr void test_msxml() { // This program will use COM CoInitializeEx(NULL, COINIT_MULTITHREADED); { // Create parser IXMLDOMDocumentPtr pXMLDoc; HRESULT hr = CoCreateInstance(__uuidof (DOMDocument), NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXMLDoc); pXMLDoc->put_validateOnParse(VARIANT_FALSE); pXMLDoc->put_resolveExternals(VARIANT_FALSE); pXMLDoc->put_preserveWhiteSpace(VARIANT_FALSE); // Open file VARIANT_BOOL bLoadOk; std::wstring sfilename = L"testfile.xml"; hr = pXMLDoc->load(_variant_t(sfilename.c_str()), &bLoadOk); // Search for node <testtag> IXMLDOMNodePtr pNode; hr = pXMLDoc->selectSingleNode(_bstr_t(L"testtag"), &pNode); // Read something _bstr_t bstrText; hr = pNode->get_text(bstrText.GetAddress()); std::string sSomething = bstrText; } // I'm finished with COM // (Don't call before all IXMLDOMNodePtr are out of scope) CoUninitialize(); }
c ++ xml visual-studio-2008 msxml
Name
source share