Posted on 2009-04-27 00:12
S.l.e!ep.¢% 阅读(434)
评论(0) 编辑 收藏 引用 所属分类:
MSXML
#include <stdlib.h>
#include <stdio.h>
#include <atlbase.h>
#import "msxml3.dll"
using namespace MSXML2;
void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);
try{
IXMLDOMDocumentPtr pXMLDoc;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument));
pXMLDoc->async = false; // default - true,
hr = pXMLDoc->load("c:\\stock.xml");
if(hr!=VARIANT_TRUE)
{
IXMLDOMParseErrorPtr pError;
pError = pXMLDoc->parseError;
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return 0;
}
CComPtr<IStream> pStream;
hr = CreateStreamOnHGlobal(NULL, true, &pStream);
hr = pXMLDoc->save(pStream.p);
LARGE_INTEGER pos;
pos.QuadPart = 0;
//the key is to reset the seek pointer
pStream->Seek((LARGE_INTEGER)pos, STREAM_SEEK_SET, NULL);
IXMLDOMDocumentPtr pXMLDocNew;
hr = pXMLDocNew.CreateInstance(__uuidof(DOMDocument));
pXMLDocNew->async = false;
hr = pXMLDocNew->load(pStream.p);
if(hr!=VARIANT_TRUE)
{
IXMLDOMParseErrorPtr pError;
pError = pXMLDocNew->parseError;
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return 0;
}
// MessageBox(NULL,(LPTSTR)pXMLDocNew->xml, "XML content",MB_OK);
IXMLDOMNodeListPtr nodeptr;
pXMLDocNew->get_childNodes(&nodeptr);
long nodesize = 0;
nodeptr->get_length(&nodesize);
IXMLDOMNodePtr xmlnode;
nodeptr->get_item(0, &xmlnode);
CComBSTR a;
xmlnode->get_baseName(&a);
USES_CONVERSION;
printf("%s\n", W2T(a));
}
catch(_com_error &e)
{
dump_com_error(e);
}
CoUninitialize();
return 0;
}