Moi!
Olen muutaman päivän ajan yritellyt päästä sisälle C++ ohjelmoinin saloihin, ympäristönä Microsoft Visula C++ 6.0 / Win32 Console). Potkun aiheeseen sain täältä... Mutta mitä 'enemmän' muutoin pääsen sisälle, sitä sekavammaksi käyvät MSXML kuviot. Elikäs: Please tell me, että millä ****lla saan ladattua XML-datan IXMLDocument-objektiin???
tässä koodisample:
// ReadTableFromPage.cpp : Defines the entry point for the console application. // #define UNICODE #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <atlbase.h> #include <atlconv.h> #include <comdef.h> #include <windows.h> #include <wininet.h> #pragma comment(lib,"wininet.lib") #import <msxml6.dll> named_guids #pragma comment(lib,"msxml6.lib") using namespace std; using namespace MSXML2; int main(int argc, char* argv[]) { USES_CONVERSION; char Buf[1024]; DWORD ReadSize; BOOL bResult; string str; HINTERNET hInternet = InternetOpenA( "UsewrAgent/1.0",INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0); if(!hInternet) { printf("hInternet Failed!\n"); return -1; } HINTERNET hConnection = InternetConnectA( hInternet, "http://www.elisanet.fi/nea.fi/tablex", INTERNET_DEFAULT_HTTPS_PORT, "","", INTERNET_SERVICE_HTTP,0,0); if (!hConnection) { InternetCloseHandle(hInternet); printf("InternetConnectA failed!\n"); return -1; } HINTERNET hFile = InternetOpenUrl( hInternet, "http://www.elisanet.fi/nea.fi/tablex", NULL,0,INTERNET_FLAG_RELOAD,0); if(!hFile) { InternetCloseHandle(hConnection); InternetCloseHandle(hInternet); printf("InternetOpenUrl Failed!\n"); return -1; } for(;;) { ReadSize = 1024; bResult = InternetReadFile( hFile,Buf,1024,&ReadSize); if(bResult && (ReadSize == 0)) break; Buf[ReadSize] = '\0'; str+=("%s", Buf); } InternetCloseHandle(hFile); InternetCloseHandle(hConnection); InternetCloseHandle(hInternet); size_t ltag; size_t utag; ltag = str.find("</table>"); utag = str.find("</TABLE>"); string stag; string etag; if(int(ltag) > -1) { stag = "<table"; etag = "/table"; } else if(int(ltag) < 0 && int(utag) > -1) { stag = "<TABLE"; etag = "/TABLE"; } else if(int(ltag) == -1 && int(utag) == -1) { printf("Requested page has no table element!\n"); return -1; } size_t spos; spos = str.find(stag); size_t epos; epos = str.find(etag); spos -= 1; epos += 7; string xmlstr; xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xmlstr += str.substr(spos, epos-spos); CComBSTR cbstrData(xmlstr.c_str()); BSTR bstrXml = cbstrData.Copy(); SysFreeString(cbstrData); xmlstr=""; HRESULT hr; hr = CoInitialize(NULL); if(FAILED (hr)) { printf("HRESULT Initialization failed!\n"); SysFreeString(bstrXml); return -1; } IXMLDOMDocumentPtr pDoc = NULL; pDoc.CreateInstance(__uuidof(DOMDocument)); if(pDoc==NULL) { printf("DOMDocument creation failed!\n"); SysFreeString(bstrXml); return -1; } CComPtr<IXMLDOMDocument> xmlDoc; xmlDoc = (IXMLDOMDocument*)pDoc; pDoc = NULL; xmlDoc->put_async(VARIANT_TRUE); xmlDoc->put_preserveWhiteSpace(VARIANT_TRUE); xmlDoc->put_validateOnParse(VARIANT_FALSE); hr = xmlDoc->loadXML(bstrXml); if(FAILED (hr)) { printf("Loading .xml data failed!\n"); SysFreeString(bstrXml); return -1; } _bstr_t xmldata; xmldata = xmlDoc->Getxml(); string chkstr = OLE2CA(xmldata); BOOL bStillNotLoaded; if(chkstr == "") { SysFreeString(xmldata); bStillNotLoaded = TRUE; } else { cout << OLE2CA(xmldata) << endl; cout << "\nXML data successfully loaded to IXMLDocument object!" << endl; SysFreeString(xmldata); SysFreeString(bstrXml); } if(bStillNotLoaded) { cout << OLE2CA(bstrXml) << endl; cout << "\nXML data still not loaded to IXMLDocument object!" << endl; SysFreeString(bstrXml); } return 0; }
Heippa taas!
ongelman ratkaisu
Tämä nyt voi olla huono ohje, mutta käytä TinyXml:ää
http://www.grinninglizard.com/tinyxml/
Aihe on jo aika vanha, joten et voi enää vastata siihen.