HRESULT XMLHelper::GetChildText(MSXML2::IXMLDOMNode *pNode, const TCHAR *pChild, BSTR *pBstr) { BSTR xpath = SysAllocString(pChild); MSXML2::IXMLDOMNode *pChildNode = 0; HRESULT hr = pNode->selectSingleNode(xpath, &pChildNode); SysFreeString(xpath); if(hr == S_OK && pChildNode) { hr = pChildNode->get_text(pBstr); pChildNode->Release(); } return hr; }
void CDialogSettings::LoadXMLSettings(MSXML2::IXMLDOMNamedNodeMap *pam,long lChildNodes) { MSXML2::IXMLDOMNode *pNode = NULL; BSTR bstrName; BSTR bstrValue; for(int i = 1;i < lChildNodes;i++) { pam->get_item(i,&pNode); pNode->get_nodeName(&bstrName); pNode->get_text(&bstrValue); bool bHandled = false; if(m_bSavePosition) { if(lstrcmpi(bstrName, SETTING_POSITION_X) == 0) { m_ptDialog.x = NXMLSettings::DecodeIntValue(bstrValue); bHandled = true; } else if(lstrcmpi(bstrName, SETTING_POSITION_Y) == 0) { m_ptDialog.y = NXMLSettings::DecodeIntValue(bstrValue); bHandled = true; } else if(lstrcmpi(bstrName, SETTING_WIDTH) == 0) { m_iWidth = NXMLSettings::DecodeIntValue(bstrValue); bHandled = true; } else if(lstrcmpi(bstrName, SETTING_HEIGHT) == 0) { m_iHeight = NXMLSettings::DecodeIntValue(bstrValue); bHandled = true; } } if(!bHandled) { /* Pass the node name and value to any descendant class to handle. */ LoadExtraXMLSettings(bstrName,bstrValue); } } m_bStateSaved = TRUE; }
void XmlParser::_parseNodeGetText(MSXML2::IXMLDOMNode *pIDOMNode, XmlNode& node) { MSXML2::IXMLDOMNode* ppFirstChild; BSTR bstrNodeType; pIDOMNode->get_firstChild(&ppFirstChild); if (ppFirstChild == NULL) return; ppFirstChild->get_nodeTypeString(&bstrNodeType); if (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)L"text") == 0 || (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)L"cdatasection") == 0)) { BSTR bstrItemText; ppFirstChild->get_text(&bstrItemText); node.SetText(wstring(bstrItemText)); } }
void XMLDOMParsingNodes(char *szFileName) { try { //Qualify namespase explicitly to avoid Compiler Error C2872 "ambiguous symbol" during linking. //Now Msxml2.dll use the "MSXML2" namespace //(see http://support.microsoft.com/default.aspx?scid=kb;en-us;316317): MSXML2::IXMLDOMDocumentPtr docPtr;//pointer to DOMDocument object MSXML2::IXMLDOMNodeListPtr NodeListPtr;//indexed access. and iteration through the collection of nodes MSXML2::IXMLDOMNodePtr DOMNodePtr;//pointer to the node MSXML2::IXMLDOMNode *pIDOMNode = NULL;//pointer to element's node MSXML2::IXMLDOMNode *pIParentNode = NULL;//pointer to parent node MSXML2::IXMLDOMNode *pIAttrNode = NULL;//pointer to attribute node MSXML2::IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;//iteration through the collection of attribute nodes MSXML2::IXMLDOMNodeList *childList=NULL;//node list containing the child nodes //Variable with the name of node to find: BSTR strFindText = L" ";//" " means to output every node //Variables to store item's name, parent, text and node type: BSTR bstrItemText,bstrItemNode, bstrItemParent,bstrNodeType; //Variables to store attribute's name,type and text: BSTR bstrAttrName, bstrAttrType, bstrAttrText; HRESULT hResult; int i = 0;//loop-index variable int n = 0;//lines counter //Initialize COM Library: CoInitialize(NULL); //Create an instance of the DOMDocument object: docPtr.CreateInstance(__uuidof(DOMDocument30)); // Load a document: _variant_t varXml(szFileName);//XML file to load _variant_t varResult((bool)TRUE);//result varResult = docPtr->load(varXml); if ((bool)varResult == FALSE) { //printf("*** Error:failed to load XML file. ***\n"); MessageBox(0,"Error: failed to load XML file. Check the file name.", \ "Load XML file",MB_OK |MB_ICONWARNING); return; } //Collect all or selected nodes by tag name: NodeListPtr = docPtr->getElementsByTagName(strFindText); //Output the number of nodes: //printf("Number of nodes: %d\n", (NodeListPtr->length)); //Output root node: docPtr->documentElement->get_nodeName(&bstrItemText); //%ls formatting is for wchar_t* parameter's type (%s for char* type): printf("\nRoot: %ls\n", bstrItemText); for(i = 0; i < (NodeListPtr->length); i++) { if (pIDOMNode) pIDOMNode->Release(); NodeListPtr->get_item(i, &pIDOMNode); if(pIDOMNode ) { pIDOMNode->get_nodeTypeString(&bstrNodeType); //We process only elements (nodes of "element" type): BSTR temp = L"element"; if (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)temp)==0) { n++;//element node's number printf("\n\n%d\n", n);//element node's number printf("Type: %ls\n", bstrNodeType); pIDOMNode->get_nodeName(&bstrItemNode); printf("Node: %ls\n", bstrItemNode); pIDOMNode->get_text(&bstrItemText); printf("Text: %ls\n", bstrItemText); pIDOMNode->get_parentNode(&pIParentNode); pIParentNode->get_nodeName(&bstrItemParent); printf("Parent: %ls\n",bstrItemParent); pIDOMNode->get_childNodes(&childList); printf("Child nodes: %d\n", (childList->length)); //Get the attributes: int j = 0;//loop-index variable long length;// number of attributes in the collection DOMNamedNodeMapPtr = pIDOMNode->attributes; hResult = DOMNamedNodeMapPtr->get_length(&length); if(SUCCEEDED(hResult)) { //Loop through the number of attributes: for(j = 0; j < length; j++) { //get attribute node: DOMNamedNodeMapPtr->get_item(j,&pIAttrNode); pIAttrNode->get_nodeTypeString(&bstrAttrType);//type as string printf("\nAttribute type: %ls\n", bstrAttrType); //pIAttrNode->get_nodeType(&bstrAttrType);//enumerated type //printf("Attribute type: %d\n", bstrAttrType); pIAttrNode->get_nodeName(&bstrAttrName); printf("Attribute name: %ls\n", bstrAttrName); pIAttrNode->get_text(&bstrAttrText); printf("Attribute value: %ls\n", bstrAttrText); } } // ::MessageBox(NULL, (LPCSTR) bstrItemText,"parsing", MB_OK); } } } //Do not forget to release interfaces: pIDOMNode->Release(); pIDOMNode = NULL; pIParentNode->Release(); pIParentNode = NULL; } catch(...) { MessageBox(NULL, ("*** Exception occurred ***"), ("Error message"), MB_OK); } CoUninitialize(); }
HRESULT XMLPaser::ND_hrGetTableValue( LPCTSTR strTable, LPCTSTR strField, UINT idxRow, TCHAR* pVal ) { HRESULT hr = E_FAIL; MSXML2::IXMLDOMNode *pNode = NULL; long idxColumn = -1; long lTableChilds = 0; long lfirstRow = 0; try { pNode = ND_pGetTableNode( strTable ); if ( pNode ) { MSXML2::IXMLDOMNodeList* pRowNodeList; pNode->get_childNodes( &pRowNodeList ); if ( pRowNodeList ) { pRowNodeList->get_length( &lTableChilds ); MSXML2::IXMLDOMNode *pRowNode; BSTR bstrName; LPSTR pszName; for ( long ll = 0; ll < lTableChilds; ll++ ) { pRowNodeList->get_item( ll, &pRowNode ); pRowNode->get_baseName( &bstrName ); // 처음 Row가 나오는 순간 UnicodeToAnsi( bstrName, &pszName ); if ( stricmp( pszName, _T("Row") ) == 0 ) { lfirstRow = ll; CoTaskMemFree( pszName ); ::SysFreeString( bstrName ); break; } CoTaskMemFree( pszName ); ::SysFreeString( bstrName ); } if ( lfirstRow >= lTableChilds ) { return ( E_FAIL ); } pRowNodeList->get_item( lfirstRow, &pRowNode ); if ( pRowNode ) { MSXML2::IXMLDOMNodeList* pCellNodeList; pRowNode->get_childNodes( &pCellNodeList ); if ( pCellNodeList ) { long lCell = 0; pCellNodeList->get_length( &lCell ); for ( long ll = 0; ll < lCell; ll++ ) { MSXML2::IXMLDOMNode *pCellNode; pCellNodeList->get_item( ll, &pCellNode ); if ( pCellNode ) { MSXML2::IXMLDOMNode *pDataNode; pCellNode->get_firstChild( &pDataNode ); if ( pDataNode ) { LPSTR pszColumnName; pDataNode->get_text( &bstrName ); UnicodeToAnsi( bstrName, &pszColumnName ); if ( stricmp( pszColumnName, strField ) == 0 ) { idxColumn = ll; } CoTaskMemFree( pszColumnName ); ::SysFreeString( bstrName ); pDataNode->Release(); } pCellNode->Release(); } } pCellNodeList->Release(); } pRowNode->Release(); } pRowNodeList->get_item( idxRow + lfirstRow + 1, &pRowNode ); if ( pRowNode ) { MSXML2::IXMLDOMNodeList* pCellNodeList; pRowNode->get_childNodes( &pCellNodeList ); if ( pCellNodeList ) { MSXML2::IXMLDOMNode *pDataNode; pCellNodeList->get_item( idxColumn, &pDataNode ); if ( pDataNode ) { BSTR bstrData; LPSTR pszData; pDataNode->get_text( &bstrData ); UnicodeToAnsi( bstrData, &pszData ); strcpy( pVal, pszData ); hr = S_OK; CoTaskMemFree( pszData ); ::SysFreeString( bstrData ); pDataNode->Release(); } pCellNodeList->Release(); } pRowNode->Release(); } pRowNodeList->Release(); } pNode->Release(); } return ( hr ); } catch(...) { return ( E_FAIL ); } }