BOOL vmsMetalinkFile::Parse(LPCSTR pszFile) { USES_CONVERSION; IXMLDOMDocumentPtr spXML; IXMLDOMNodePtr spNode; spXML.CreateInstance (__uuidof (DOMDocument)); if (spXML == NULL) return FALSE; spXML->put_async (FALSE); VARIANT_BOOL bRes; spXML->load (COleVariant (pszFile), &bRes); if (bRes == FALSE) return FALSE; spXML->selectSingleNode (L"metalink", &spNode); if (spNode == NULL) return FALSE; IXMLDOMNodeListPtr spNodeList; spNode->get_childNodes (&spNodeList); if (spNodeList == NULL) return FALSE; IXMLDOMNodePtr spItem; BOOL bHasOkFilesNode = FALSE; while (SUCCEEDED (spNodeList->nextNode (&spItem)) && spItem != NULL) { CComBSTR bstrName; spItem->get_nodeName (&bstrName); if (bstrName == L"description") { CComBSTR bstrText; spItem->get_text (&bstrText); m_strDescription = W2A (bstrText); } else if (bstrName == L"files") { if (ReadFilesNode (spItem)) bHasOkFilesNode = TRUE; } spItem = NULL; } return bHasOkFilesNode; }
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 LoadXMLDoc(LPCWSTR file, BDADEVICES& d) { IXMLDOMDocumentPtr Document; HRESULT hr; d.reset(); hr = Document.CreateInstance(CLSID_DOMDocument30); if(hr==S_OK) { VARIANT_BOOL success; hr = Document->load(_variant_t(file),&success); if(hr==S_OK&&success==VARIANT_TRUE) { IXMLDOMElementPtr root; IXMLDOMElementPtr bda_source; IXMLDOMElementPtr bda_reciever; IXMLDOMNodePtr bda_source_node; IXMLDOMNodePtr bda_reciever_node; hr = Document->get_documentElement(&root); hr = root->selectSingleNode(_bstr_t(L"kscategory_bda_network_tuner"),&bda_source_node); hr = root->selectSingleNode(_bstr_t(L"kscategory_bda_receiver_component"),&bda_reciever_node); bda_source = bda_source_node; bda_reciever = bda_reciever_node; GetDeviceList(bda_source,d.bda_source); GetDeviceList(bda_reciever,d.bda_reciever); } else { return false; } } else { return false; } return false; }
bool ParseXmlFile( TCHAR* pszFileName ) { if( NULL == pszFileName ) { return false; } HRESULT hResult; // DOMドキュメントの作成 IXMLDOMDocumentPtr pXMLDOMDocument; hResult = pXMLDOMDocument.CreateInstance(CLSID_DOMDocument); if( FAILED(hResult) ) { assert( !"DOMドキュメントの作成に失敗" ); return false; } // XMLファイル読み込み pXMLDOMDocument->put_async( VARIANT_FALSE ); // load関数がロードを完了するまで待つようにする。 VARIANT_BOOL varbResult; hResult = pXMLDOMDocument->load( CComVariant(pszFileName), &varbResult ); if( FAILED(hResult) || !varbResult) { assert( !"XMLファイル読み込みに失敗" ); return false; } // std::vector<CComBSTR> vbstrValue; std::vector<CComBSTR> vbstrValueName; std::vector<CComBSTR> vbstrValueScreenName; std::vector<CComBSTR> vbstrValueText; std::vector<CComBSTR> vbstrValueDate; /* // author要素の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("author"), _T(""), vbstrValue ) ) { assert( !"author要素の値の取得に失敗" ); return false; } std::cout << "author要素の値" << std::endl; for( unsigned int ui = 0; ui < vbstrValue.size(); ++ui ) { std::cout << "\t" << _com_util::ConvertBSTRToString(vbstrValue[ui]) << std::endl; } */ /* // book要素のid属性の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("book"), _T("id"), vbstrValue ) ) { assert( !"book要素のid属性の値の取得に失敗" ); return false; } std::cout << "book要素のid属性の値" << std::endl; for( unsigned int ui = 0; ui < vbstrValue.size(); ++ui ) { std::cout << "\t" << _com_util::ConvertBSTRToString((TCHAR*)vbstrValue[ui]) << std::endl; } */ // name要素の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("name"), _T(""), vbstrValueName ) ) { assert( !"name要素の値の取得に失敗" ); return false; } // screen_name要素の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("screen_name"), _T(""), vbstrValueScreenName ) ) { assert( !"screen_name要素の値の取得に失敗" ); return false; } // created_at要素の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("created_at"), _T(""), vbstrValueDate ) ) { assert( !"created_at要素の値の取得に失敗" ); return false; } // text要素の値の取得 if( !GetXmlValue( pXMLDOMDocument, _T("text"), _T(""), vbstrValueText ) ) { assert( !"text要素の値の取得に失敗" ); return false; } std::cout << "つぶやきの表示テスト\n" << std::endl; for( unsigned int ui = 0; ui < vbstrValueName.size(); ++ui ) { std::cout << ui+1 << ")" << _com_util::ConvertBSTRToString(vbstrValueName[ui]) << " " << _com_util::ConvertBSTRToString(vbstrValueScreenName[ui]) << std::endl; std::cout << " " << _com_util::ConvertBSTRToString(vbstrValueText[ui]) << std::endl; std::cout << " " << _com_util::ConvertBSTRToString(vbstrValueDate[ui]) << "\n" << std::endl; } return true; }
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; }