HRESULT XMLHelper::GetAttributeFromNode(IXMLDOMNode *pNode, BSTR bstrAttrName, VARIANT * varAttrValue) { HRESULT hr = S_OK; IXMLDOMNamedNodeMap *pAttributes = NULL; IXMLDOMNode *pAttrNode = NULL; CHK_HR(pNode->get_attributes(&pAttributes)); if (pAttributes) { CHK_HR(pAttributes->getNamedItem(bstrAttrName, &pAttrNode)); if (pAttrNode) { CHK_HR(pAttrNode->get_nodeValue(varAttrValue)); } } else { hr = S_FALSE; } CleanUp: SAFE_RELEASE(pAttributes); SAFE_RELEASE(pAttrNode); return hr; }
//-------------------------------------------------------------------------------------- HRESULT CRatingsDB::GetAttribFromNode( IXMLDOMNode* pNode, WCHAR* strAttrib, WCHAR* strDest, int cchDest ) { IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = nullptr; BSTR bstrAttributeName = ::SysAllocString( strAttrib ); IXMLDOMNode* pIXMLDOMNode = nullptr; bool bFound = false; HRESULT hr; VARIANT v; hr = pNode->get_attributes( &pIXMLDOMNamedNodeMap ); if(SUCCEEDED(hr) && pIXMLDOMNamedNodeMap) { hr = pIXMLDOMNamedNodeMap->getNamedItem( bstrAttributeName, &pIXMLDOMNode ); if(SUCCEEDED(hr) && pIXMLDOMNode) { pIXMLDOMNode->get_nodeValue(&v); if( SUCCEEDED(hr) && v.vt == VT_BSTR ) { wcscpy_s( strDest, cchDest, v.bstrVal ); bFound = true; } VariantClear(&v); SAFE_RELEASE( pIXMLDOMNode ); } SAFE_RELEASE( pIXMLDOMNamedNodeMap ); } ::SysFreeString(bstrAttributeName); bstrAttributeName = nullptr; if( !bFound ) return E_FAIL; else return S_OK; }
int ZXML::GetAttributeText( IXMLDOMNode* pNode, LPSTR strAttrName, LPSTR strRet ) { wchar_t wstrAttr[128]; IXMLDOMNode* pAttrNode = NULL; IXMLDOMNamedNodeMap* pMap = NULL; VARIANT varValue; try { int n = mbstowcs( wstrAttr, strAttrName, 128 ); pNode->get_attributes( &pMap ); pMap->getNamedItem( wstrAttr, &pAttrNode ); pAttrNode->get_nodeValue( &varValue ); strcpy( strRet,(const char*)_bstr_t(varValue.bstrVal,FALSE) ); // wcstombs( strRet, varValue.bstrVal , 128 ); S_REL( pAttrNode ); S_REL( pMap ); return TRUE; } catch(...) { S_REL( pAttrNode ); S_REL( pMap ); return FALSE; } }
bool CXML::GetNodeAttributeValue(IXMLDOMNode ** ppNode,CString csAttributeName,CString &rcsValue) { IXMLDOMNode *pNodeAttribute = NULL; IXMLDOMNamedNodeMap *pNodeMap = NULL; CString csTemp; if(!ppNode || !(*ppNode)) return false; m_hr = (*ppNode)->get_attributes(&pNodeMap); if (SUCCEEDED(m_hr) == 0 || pNodeMap == NULL) return false; _bstr_t bstrAttributeName(csAttributeName); m_hr = pNodeMap->getNamedItem (bstrAttributeName,&pNodeAttribute); pNodeMap->Release(); if (SUCCEEDED(m_hr) == 0 || (pNodeAttribute == NULL)) return false; _variant_t vtAttributeVal; m_hr = pNodeAttribute->get_nodeValue(&vtAttributeVal); pNodeAttribute->Release(); if (SUCCEEDED(m_hr) == 0) return false; rcsValue = vtAttributeVal.bstrVal; return true; }
int KXML::GetAttributeText(IXMLDOMNode* pNode, LPSTR strAttrName, LPSTR strRet){ wchar_t wstrAttr[128]; IXMLDOMNode* pAttrNode = NULL; IXMLDOMNamedNodeMap* pMap = NULL; VARIANT varValue; try{ //int n = mbstowcs(wstrAttr, strAttrName, 128); //int n = mbstowcs_s(wstrAttr, strAttrName, 128); // TODO : error test size_t convertedSize; int n = mbstowcs_s(&convertedSize, wstrAttr, strlen(strAttrName) + 1, strAttrName, 128); pNode->get_attributes(&pMap); pMap->getNamedItem(wstrAttr, &pAttrNode); pAttrNode->get_nodeValue(&varValue); strcpy_s(strRet, 128, (const char*)_bstr_t(varValue.bstrVal, FALSE)); // wcstombs(strRet, varValue.bstrVal, 128) REL(pAttrNode); REL(pMap); return TRUE; } catch (...){ REL(pAttrNode); REL(pMap); return FALSE; } }
static bool XMLGetAttrTrim( //return true only if an attribute with name pszwAttr found IXMLDOMNode* pNode, //node where to get attribute from const wchar_t* pszwAttr, //Name of attribute to get wchar_t szAttrVal[MAX_PATH] //buffer for result ) { HRESULT hr; VARIANT val; wchar_t* pszWchar; int nLen; IXMLDOMNode* pAttr; IXMLDOMNamedNodeMap* pAList; szAttrVal[0] = 0; pAList = NULL; pAttr = NULL; pNode->get_attributes(&pAList); if(pAList) { pAList->getNamedItem((wchar_t*)pszwAttr, &pAttr); FC_RELEASE_PTR(pAList); } if(!pAttr) return false; VariantInit(&val); hr = pAttr->get_nodeValue(&val); FC_RELEASE_PTR(pAttr); if(FAILED(hr) || val.vt!=VT_BSTR) { VariantClear(&val); return false; } //trim the string: pszWchar = val.bstrVal; while(_istspace(*pszWchar)) pszWchar++; for(nLen=wcslen(pszWchar);nLen>0 && _istspace(pszWchar[nLen-1]);nLen--) pszWchar[nLen-1] = 0; wcsncpy(szAttrVal, pszWchar, MAX_PATH); szAttrVal[MAX_PATH-1] = 0; VariantClear(&val); return true; }
inline bool GetXmlAttribute(IXMLDOMNamedNodeMap *attributes, const wchar_t *name, CGrVector &v) { IXMLDOMNode *node; CComBSTR nameB = name; attributes->getNamedItem(nameB, &node); if(node == NULL) return false; CComVariant valueV; node->get_nodeValue(&valueV); std::wstringstream str(valueV.bstrVal); double x, y, z, w; str >> x >> y >> z >> w; v.Set(x, y, z, w); return true; }
//-------------------------------------------------------------------------------------- BOOL ConfGetNodeAttributeW(IXMLDOMNode *pIDOMNode, PWSTR name, PWSTR *value) { BOOL bRet = FALSE; IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL; // query attributes map HRESULT hr = pIDOMNode->get_attributes(&pIXMLDOMNamedNodeMap); if (SUCCEEDED(hr) && pIXMLDOMNamedNodeMap) { IXMLDOMNode *pIDOMAttrNode = NULL; // query attribute node hr = pIXMLDOMNamedNodeMap->getNamedItem(name, &pIDOMAttrNode); if (SUCCEEDED(hr) && pIDOMAttrNode) { VARIANT varValue; hr = pIDOMAttrNode->get_nodeValue(&varValue); if (FAILED(hr)) { DbgMsg(__FILE__, __LINE__, "pIDOMAttrNode->get_nodeValue() ERROR 0x%.8x\n", hr); goto free; } BSTR val = _bstr_t(varValue); DWORD Len = (wcslen((PWSTR)val) + 1) * sizeof(WCHAR); if (*value = (PWSTR)M_ALLOC(Len)) { ZeroMemory(*value, Len); wcscpy(*value, (PWSTR)val); bRet = TRUE; } else { DbgMsg(__FILE__, __LINE__, "M_ALLOC() ERROR %d\n", GetLastError()); } free: pIDOMAttrNode->Release(); pIDOMAttrNode = NULL; } pIXMLDOMNamedNodeMap->Release(); pIXMLDOMNamedNodeMap = NULL; } return bRet; }
bool CNtlXMLDoc::GetTextWithAttributeName(IXMLDOMNode* pNode, WCHAR* pwszAttributeName, WCHAR* pwszResultText, int nBufferSizeInWChars) { if (NULL == pNode || NULL == pwszAttributeName || NULL == pwszResultText) { // NtlAssertFail("NULL == pNode || NULL == pwszAttributeName || NULL == pwszResultText"); return false; } if (0 >= nBufferSizeInWChars) { // NtlAssertFail("0 >= nBufferSizeInWChars"); return false; } IXMLDOMNamedNodeMap* pMap = NULL; pNode->get_attributes(&pMap); if (NULL == pMap) { // NtlAssertFail("Couldn't get the attribute list from the given IXMLDOMNode."); return false; } IXMLDOMNode* pVirtualNode = NULL; pMap->getNamedItem(pwszAttributeName, &pVirtualNode); if (NULL == pVirtualNode) { // NtlAssertFail("Couldn't find the given attribute name."); return false; } VARIANT var; VariantInit(&var); pVirtualNode->get_nodeValue(&var); if (wcslen(V_BSTR(&var)) >= (size_t)nBufferSizeInWChars) { // NtlAssertFail("The buffer size is not enough to take the whole attribute value."); return false; } wcscpy_s(pwszResultText, nBufferSizeInWChars, V_BSTR(&var)); return true; }
inline bool GetXmlAttribute(IXMLDOMNamedNodeMap *attributes, const wchar_t *name, CGrTransform &transform) { IXMLDOMNode *node; CComBSTR nameB = name; attributes->getNamedItem(nameB, &node); if(node == NULL) return false; CComVariant valueV; node->get_nodeValue(&valueV); std::wstringstream str(valueV.bstrVal); for(int r=0; r<4; r++) { for (int c=0; c<4; c++) { str >> transform.M(r, c); } } return true; }
/////////////////////////////////////////////////////////////// // 非公开方法 /////////////////////////////////////////////////////////////// // 读结点Text(数据结点) _variant_t ScanParamXML::ReadNodeText( IXMLDOMNode *const pTheNode ) { _variant_t varVal = 0; try { IXMLDOMNode *pTextNode; VARIANT_BOOL bOk; pTheNode->hasChildNodes( &bOk ); if( bOk ) { // 有子结点(即Text结点,保存着数据) pTheNode->get_firstChild( &pTextNode ); pTextNode->get_nodeValue( &varVal ); } } catch( ... ) { // 无处理 } return varVal; }
STDMETHODIMP CStrokeRecognition::put_RecognitionParam(BSTR newVal) { double Segment_Error_Threshold; double Arc_Error_Threshold; double Arc_Min_Length; double Arc_Min_Curve; double Stroke_Min_Length; double Min_Turning_Angle; double Segmentation_Penalty; VARIANT value; BSTR nodeName; long n; IXMLDOMDocument* pDoc; IXMLDOMNodeList* pNodeList; IXMLDOMNode* pNode; IXMLDOMNode* pParamNode; IXMLDOMNode* pAttrNode; IXMLDOMNamedNodeMap* pNodeMap; VARIANT_BOOL bLoaded; GetFittingParam(Segment_Error_Threshold, Arc_Error_Threshold, Arc_Min_Length, Arc_Min_Curve, Stroke_Min_Length, Min_Turning_Angle, Segmentation_Penalty); if (SUCCEEDED(CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (LPVOID*)&pDoc))) { pDoc->put_async(VARIANT_FALSE); pDoc->loadXML(newVal, &bLoaded); if (bLoaded == VARIANT_TRUE) { if (SUCCEEDED(pDoc->get_childNodes(&pNodeList))) { pParamNode = NULL; pNodeList->get_length(&n); for (int i = 0; i < n; i++) { if (SUCCEEDED(pNodeList->get_item(i, &pNode))) { nodeName = NULL; if (SUCCEEDED(pNode->get_nodeName(&nodeName))) { if (CComBSTR(nodeName) == L"param") { pParamNode = pNode; pParamNode->AddRef(); break; } SysFreeString(nodeName); } pNode->Release(); } } pNodeList->Release(); if (pParamNode != NULL) { if (SUCCEEDED(pParamNode->get_attributes(&pNodeMap))) { if (SUCCEEDED(pNodeMap->getNamedItem(L"Segment_Error_Threshold", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Segment_Error_Threshold = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Arc_Error_Threshold", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Arc_Error_Threshold = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Arc_Min_Length", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Arc_Min_Length = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Arc_Min_Curve", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Arc_Min_Curve = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Stroke_Min_Length", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Stroke_Min_Length = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Min_Turning_Angle", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Min_Turning_Angle = _wtof(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"Segmentation_Penalty", &pAttrNode))) { pAttrNode->get_nodeValue(&value); Segmentation_Penalty = _wtof(value.bstrVal); pAttrNode->Release(); } pNodeMap->Release(); } pParamNode->Release(); } } } pDoc->Release(); } SetFittingParam(Segment_Error_Threshold, Arc_Error_Threshold, Arc_Min_Length, Arc_Min_Curve, Stroke_Min_Length, Min_Turning_Angle, Segmentation_Penalty); return S_OK; }
void CStrokeRecognition::DecodePointFromXML(std::vector<POINT> &rgPoints, BSTR strXML) { IXMLDOMDocument* pDoc; IXMLDOMNodeList* pNodeList; IXMLDOMNode* pNode; IXMLDOMNode* pInputNode; IXMLDOMNode* pAttrNode; IXMLDOMNamedNodeMap* pNodeMap; VARIANT_BOOL bLoaded; VARIANT value; BSTR nodeName; POINT pt; long n; if (SUCCEEDED(CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (LPVOID*)&pDoc))) { pDoc->put_async(VARIANT_FALSE); pDoc->loadXML(strXML, &bLoaded); if (bLoaded == VARIANT_TRUE) { if (SUCCEEDED(pDoc->get_childNodes(&pNodeList))) { pInputNode = NULL; pNodeList->get_length(&n); for (int i = 0; i < n; i++) { if (SUCCEEDED(pNodeList->get_item(i, &pNode))) { nodeName = NULL; if (SUCCEEDED(pNode->get_nodeName(&nodeName))) { if (CComBSTR(nodeName) == L"input") { pInputNode = pNode; pInputNode->AddRef(); break; } SysFreeString(nodeName); } pNode->Release(); } } pNodeList->Release(); if (pInputNode != NULL) { if (SUCCEEDED(pInputNode->get_childNodes(&pNodeList))) { pNodeList->get_length(&n); for (int i = 0; i < n; i++) { if (SUCCEEDED(pNodeList->get_item(i, &pNode))) { pt.x = 0; pt.y = 0; if (SUCCEEDED(pNode->get_attributes(&pNodeMap))) { if (SUCCEEDED(pNodeMap->getNamedItem(L"x", &pAttrNode))) { pAttrNode->get_nodeValue(&value); pt.x = _wtoi(value.bstrVal); pAttrNode->Release(); } if (SUCCEEDED(pNodeMap->getNamedItem(L"y", &pAttrNode))) { pAttrNode->get_nodeValue(&value); pt.y = _wtoi(value.bstrVal); pAttrNode->Release(); } pNodeMap->Release(); } rgPoints.push_back(pt); pNode->Release(); } } pNodeList->Release(); } pInputNode->Release(); } } } pDoc->Release(); } }
bool CNtlXMLDoc::GetTextWithAttributeName(IXMLDOMNode* pNode, char* pszAttributeName, char* pszResultText, int nBufferSizeInBytes) { if (NULL == pNode || NULL == pszAttributeName || NULL == pszResultText) { // NtlAssertFail("NULL == pNode || NULL == pszAttributeName || NULL == pszResultText"); return false; } if (0 >= nBufferSizeInBytes) { // NtlAssertFail("0 >= nBufferSizeInBytes"); return false; } int nRequiredBytes = 0; nRequiredBytes = MultiByteToWideChar(GetACP(), 0, pszAttributeName, -1, NULL, 0); if (0 == nRequiredBytes) { // NtlAssertFail("The given attribute name can't be converted into WCHAR type for some reason."); return false; } if (nRequiredBytes > (CNtlXMLDoc::MAX_ATTRIBUTE_NAME_IN_WCHAR + 1)) { // NtlAssertFail("The given attribute name is too long."); return false; } WCHAR pwszAttributeNameInWChar[CNtlXMLDoc::MAX_ATTRIBUTE_NAME_IN_WCHAR + 1]; int nUsedBufferSize = MultiByteToWideChar(GetACP(), 0, pszAttributeName, -1, pwszAttributeNameInWChar, (CNtlXMLDoc::MAX_ATTRIBUTE_NAME_IN_WCHAR + 1)); if (0 == nUsedBufferSize) { // NtlAssertFail("The given attribute name couldn't be converted into WCHAR type for some reason."); return false; } IXMLDOMNamedNodeMap* pMap = NULL; pNode->get_attributes(&pMap); if (NULL == pMap) { // NtlAssertFail("Couldn't get the attribute list from the given IXMLDOMNode."); return false; } IXMLDOMNode* pVirtualNode = NULL; pMap->getNamedItem(pwszAttributeNameInWChar, &pVirtualNode); if (NULL == pVirtualNode) { // NtlAssertFail("Couldn't find the given attribute name."); return false; } VARIANT var; VariantInit(&var); pVirtualNode->get_nodeValue(&var); nRequiredBytes = WideCharToMultiByte(::GetACP(), 0, V_BSTR(&var), -1, pszResultText, 0, NULL, NULL); if (nRequiredBytes > nBufferSizeInBytes) { // NtlAssertFail("The buffer size is not enough to take the whole attribute value."); return false; } WideCharToMultiByte(GetACP(), 0, V_BSTR(&var), -1, pszResultText, nBufferSizeInBytes, NULL, NULL); return true; }
/**Read the XML config file. Currently contains keyboard choices.*/ void read_config_file() { TrainerConfig *result = new TrainerConfig(); CoInitialize(NULL); //read XML MSXML2::IXMLDOMDocumentPtr spXMLDoc; spXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); if (!spXMLDoc->load("ent-config.xml")) { write_text_to_log_file("No config found, using defaults"); config = result; //the default config } IXMLDOMNodeListPtr nodes = spXMLDoc->selectNodes(L"//ent-config/keys/key"); long length; nodes->get_length(&length); for (int i = 0; i < length; i++) { IXMLDOMNode *node; nodes->get_item(i, &node); IXMLDOMNamedNodeMap *attribs; node->get_attributes(&attribs); long length_attribs; attribs->get_length(&length_attribs); char *attrib_key_func = NULL; char *attrib_key_value = NULL; for (long j = 0; j < length_attribs; j++) { IXMLDOMNode *attribNode; attribs->get_item(j, &attribNode); attribNode->get_nodeName(&bstr); if (wcscmp(bstr, L"function") == 0) { VARIANT var; VariantInit(&var); attribNode->get_nodeValue(&var); attrib_key_func = _com_util::ConvertBSTRToString(V_BSTR(&var)); } else if (wcscmp(bstr, L"value") == 0) { VARIANT var; VariantInit(&var); attribNode->get_nodeValue(&var); attrib_key_value = _com_util::ConvertBSTRToString(V_BSTR(&var)); } SysFreeString(bstr); attribNode->Release(); } if (attrib_key_func != NULL && attrib_key_value != NULL) { result->get_key_config()->set_key(attrib_key_func, attrib_key_value); } delete attrib_key_func; delete attrib_key_value; attribs->Release(); node->Release(); } //nodes->Release(); //don't do this, it crashes on exit spXMLDoc.Release(); CoUninitialize(); config = result; }