BOOL CDlgExportDownloads::ExportDownloads_ToDLInfoListFile(LPCSTR pszFile, DLDS_LIST* pvpDlds, BOOL bAppend) { IXMLDOMDocumentPtr spXML; IXMLDOMNodePtr spNode, spNode2; spXML.CreateInstance (__uuidof (DOMDocument)); if (spXML == NULL) return FALSE; spXML->put_async (FALSE); VARIANT_BOOL bRes = FALSE; if (bAppend) { spXML->load (COleVariant (pszFile), &bRes); if (bRes) { spXML->selectSingleNode (L"FDM_Downloads_Info_List", &spNode); if (spNode == NULL) bRes = FALSE; } } if (bRes == FALSE) { spXML->createNode (COleVariant ((long)NODE_ELEMENT), L"FDM_Downloads_Info_List", NULL, &spNode); spXML->appendChild (spNode, &spNode2); } for (size_t i = 0; i < pvpDlds->size (); i++) { ExportDownload_ToXML (spXML, spNode, pvpDlds->at (i)); } CComBSTR bstr; spXML->get_xml (&bstr); CString str = bstr; str.Replace ("><", ">\n<"); bstr = str; spXML->loadXML (bstr, &bRes); spXML->save (COleVariant (pszFile)); return TRUE; }
bool SaveXMLDoc(LPCWSTR file, BDADEVICES& d) { IXMLDOMDocumentPtr Document; IXMLDOMElementPtr root; IXMLDOMElementPtr bda_source; IXMLDOMElementPtr bda_reciever; HRESULT hr; hr = Document.CreateInstance(CLSID_DOMDocument30); if(hr==S_OK) { // these methods should not fail so don't inspect result Document->put_async(VARIANT_FALSE); Document->put_validateOnParse(VARIANT_FALSE); Document->put_resolveExternals(VARIANT_FALSE); CreatePI(Document); CreateElement(Document,L"bdainf",&root); CreateElement(Document,L"kscategory_bda_network_tuner",&bda_source); CreateElement(Document,L"kscategory_bda_receiver_component",&bda_reciever); CreateDeviceList(Document,bda_source,d.bda_source); CreateDeviceList(Document,bda_reciever,d.bda_reciever); AppendChild(bda_source,root); AppendChild(bda_reciever,root); AppendChild(root,Document); hr = Document->save(_variant_t(file)); if(hr==S_OK) { return true; } else { //notify error return false; } } else { //notify error return false; } }
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("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); } catch(_com_error &e) { dump_com_error(e); } CoUninitialize(); return 0; }