BOOL WXmlParse::readObject(std::vector<ExternalObject *> &objTree) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 3 : 递归获取对象树 elementNodes->get_item(3, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { ExternalObject *objTmp = new ExternalObject(); subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); readObject(objTmp, xmlSubNode); objTree.push_back(objTmp); } return TRUE; }
BOOL WXmlParse::readExtendModName(std::vector<std::pair<string, string> > &modNames) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 3 : 扩展模块列表 elementNodes->get_item(1, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr nameNode = elementAttributes->getNamedItem("name"); MSXML2::IXMLDOMNodePtr descNode = elementAttributes->getNamedItem("description"); modNames.push_back(std::make_pair(nameNode->text, descNode->text)); } return TRUE; }
BOOL WXmlParse::readCalculateParams(WCalcaulateParam &calParam) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); if (m_strFileName.IsEmpty()) { return FALSE; } doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 0 : elementNodes->get_item(2, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr proNode = elementAttributes->getNamedItem("name"); string proName = proNode->text; MSXML2::IXMLDOMNodeListPtr subAttrNodes; xmlSubNode->get_childNodes(&subAttrNodes); subAttrNodes->get_length(&attrLength); // ASSERT(attrLength == 2); MSXML2::IXMLDOMNodePtr descNode; MSXML2::IXMLDOMNodePtr stateNode; subAttrNodes->get_item(0, &descNode); subAttrNodes->get_item(1, &stateNode); std::vector<string> vecParam; string descText = descNode->text; string stateText = stateNode->text; vecParam.push_back(proName); vecParam.push_back(descText); vecParam.push_back(stateText); calParam.addParam(vecParam); } return TRUE; }
BOOL WXmlParse::readParam(std::vector<WParamDefine *> &outParams) { MSXML2::IXMLDOMDocumentPtr doc; MSXML2::IXMLDOMElementPtr docRoot; MSXML2::IXMLDOMElementPtr docElement; MSXML2::IXMLDOMNodeListPtr elementNodes; MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodePtr xmlNode; MSXML2::IXMLDOMNodePtr xmlSubNode; MSXML2::IXMLDOMNodePtr xmlAttrNode; doc.CreateInstance(__uuidof(DOMDocument30)); if (m_strFileName.IsEmpty()) { return FALSE; } doc->load((_variant_t)m_strFileName); docRoot = doc->GetdocumentElement();//获得根节点; docRoot->get_childNodes(&elementNodes); LONG length, subLength, attrLength; elementNodes->get_length(&length); MSXML2::IXMLDOMNodeListPtr subElementNodes; // 0 : elementNodes->get_item(0, &xmlNode); xmlNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&subLength); for (int j = 0; j < subLength;j++) { subElementNodes->get_item(j, &xmlSubNode);//获得某个属性 xmlSubNode->get_attributes(&elementAttributes); MSXML2::IXMLDOMNodePtr proNode = elementAttributes->getNamedItem("name"); string proName = proNode->text; WParamDefine *paramTest = new WParamDefine(proName); MSXML2::IXMLDOMNodeListPtr subAttrNodes; xmlSubNode->get_childNodes(&subAttrNodes); subAttrNodes->get_length(&attrLength); for (int k = 0;k < attrLength;k++) { subAttrNodes->get_item(k, &xmlAttrNode); string attrName = xmlAttrNode->nodeName; string attrText = xmlAttrNode->text; paramTest->addValue(attrName, attrText); } outParams.push_back(paramTest); } return TRUE; }
// 获取当前节点的最后一个Element子节点 IBpXMLNode* CBpXMLNode::GetLastElementChild() const { if (m_pDOMNode == NULL) return NULL; if (m_pDOMNode->GetnodeType() == MSXML2::NODE_ELEMENT) { MSXML2::IXMLDOMNodeListPtr pNodeList = m_pDOMNode->GetchildNodes(); if (pNodeList == NULL) return NULL; long nListLen = 0; pNodeList->get_length(&nListLen);; MSXML2::IXMLDOMNodePtr pNode = NULL; for (long i = 0; i < nListLen; i++) { pNodeList->get_item(nListLen - 1 - i, &pNode); if (pNode->GetnodeType() == MSXML2::NODE_ELEMENT) { CBpXMLNode *pNodeCreated = new CBpXMLNode(); pNodeCreated->m_pDOMNode = pNode; return (IBpXMLNode*)pNodeCreated; } } } return NULL; }
void XmlParser::Parse(NodeCallback callback, void *data) { MSXML2::IXMLDOMNodeListPtr NodeListPtr; BSTR strFindText = L" "; MSXML2::IXMLDOMNode *pIDOMNode = NULL; BSTR bstrNodeType; assert(m_domDocument != NULL); NodeListPtr = m_domDocument->getElementsByTagName(strFindText); for(int i = 0; i < NodeListPtr->length; i++) { if (pIDOMNode) pIDOMNode->Release(); NodeListPtr->get_item(i, &pIDOMNode); pIDOMNode->get_nodeTypeString(&bstrNodeType); if (lstrcmp((LPCTSTR)bstrNodeType, (LPCTSTR)L"element") != 0) continue; XmlNode node; _parseNode(pIDOMNode, node); if (callback(node, data) == false) break; } if (pIDOMNode) pIDOMNode->Release(); }
//遍历指定节点列表的数据 BOOL CXmlBase::FetchNodeList(LPCTSTR lpszXmlPathFile, LPCTSTR lpszNamespace , LPCTSTR lpszNodeListXPath, DWORD dwFlag) { MSXML2::IXMLDOMDocument2Ptr pDoc = NULL; MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; long lNodeCount = 0; HRESULT hr = S_OK; if (!IsAccessibleFile(lpszXmlPathFile)) return FALSE; pDoc = GetXmlDocPtrByFile(lpszXmlPathFile, lpszNamespace); if (pDoc == NULL) return FALSE; pNodeList = GetNodeListPtr(lpszNodeListXPath, pDoc); if (pNodeList == NULL) return FALSE; lNodeCount = pNodeList->Getlength(); for (int i=0; i<lNodeCount; i++) { hr = pNodeList->get_item(i, &pNode); if (FAILED(hr) || NULL == pNode) return FALSE; if (!OnFetchNodeList(pNode, dwFlag)) return FALSE; } return TRUE;//(lNodeCount != 0); }
//遍历指定节点列表的数据 BOOL CXmlBase::FetchNodeList2(MSXML2::IXMLDOMNodePtr pParentNode, DWORD dwFlag) { MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; long lNodeCount = 0; HRESULT hr = S_OK; if (NULL == pParentNode) { return FALSE; } pNodeList = GetNodeListPtr(pParentNode); if (pNodeList == NULL) { return FALSE; } lNodeCount = pNodeList->Getlength(); for (long i=0; i<lNodeCount; i++) { hr = pNodeList->get_item(i, &pNode); if (FAILED(hr) || NULL == pNode) { return FALSE; } if (!OnFetchNodeList2(pNode, dwFlag)) { return FALSE; } } return TRUE; }
MTConnectDataModel MTConnectDeviceParser::ReadDeviceDescription(std::string filename) { MTConnectDataModel dataItems; dataItems.clear(); if(GetFileAttributesA(filename.c_str())== INVALID_FILE_ATTRIBUTES) { filename= ::ExeDirectory() + filename; } if(GetFileAttributesA(filename.c_str())== INVALID_FILE_ATTRIBUTES) throw std::exception("MTConnectDeviceParser::ReadDeviceDescription invalid devices file"); _devicesfilename = filename; try{ ParseXMLDocument(_devicesfilename.c_str()); MSXML2::IXMLDOMNodePtr root = m_pXMLDoc->GetdocumentElement(); MSXML2::IXMLDOMNodeListPtr nodes = root->selectNodes(_bstr_t("//DataItem")); for(int i=0; i< nodes->length; i++) { MSXML2::IXMLDOMNodePtr pNode = NULL; CDataItem dataItem; nodes->get_item(i, &pNode); dataItem.name = (LPCSTR) GetAttribute(pNode, "name"); dataItem.category = (LPCSTR) GetAttribute(pNode, "category"); dataItem.id = (LPCSTR) GetAttribute(pNode, "id"); dataItem.type = (LPCSTR) GetAttribute(pNode, "type"); dataItem.subType = (LPCSTR) GetAttribute(pNode, "subType"); dataItem.units = (LPCSTR) GetAttribute(pNode, "units"); dataItem.nativeUnits = (LPCSTR) GetAttribute(pNode, "nativeUnits"); if(dataItem.type == "ASSET_CHANGED") { dataItem.category = "ASSET_CHANGED"; } dataItem.category=MakeLower(dataItem.category); if(!dataItem.name.empty()) { dataItems[dataItem.name]=dataItem; } // Could get name or id via SHDR else if(!dataItem.id.empty()) { dataItems[dataItem.id]=dataItem; } else { continue; } } } catch(_com_error error) { std::cout << "MTConnectDeviceParser::ReadDeviceDescription" << error.ErrorMessage(); } return dataItems; }
BOOL WXmlParse::readObject(ExternalObject *objRet, MSXML2::IXMLDOMNodePtr &xmlSubNode) { MSXML2::IXMLDOMNamedNodeMapPtr elementAttributes; MSXML2::IXMLDOMNodeListPtr subElementNodes; MSXML2::IXMLDOMNodeListPtr subSubElementNodes; MSXML2::IXMLDOMNodePtr xmlNodeTmp; xmlSubNode->get_attributes(&elementAttributes); LONG attrLength; attrLength = elementAttributes->Getlength(); MSXML2::IXMLDOMNodePtr nameNode = elementAttributes->getNamedItem("name"); MSXML2::IXMLDOMNodePtr clsNode = elementAttributes->getNamedItem("type"); objRet->objName = nameNode->text; objRet->clsName = clsNode->text; LONG length, subLength; xmlSubNode->get_childNodes(&subElementNodes); subElementNodes->get_length(&length); objRet->attri.clear(); for (int i = 0;i < length;i++) { subElementNodes->get_item(i, &xmlNodeTmp); string childName = xmlNodeTmp->nodeName; if (strcmp(xmlNodeTmp->nodeName,"child") != 0) { xmlNodeTmp->get_attributes(&elementAttributes); if (!elementAttributes) return FALSE; MSXML2::IXMLDOMNodePtr typeNode = elementAttributes->getNamedItem("type"); objRet->attri.push_back(std::make_pair(xmlNodeTmp->nodeName, std::make_pair(typeNode->text, xmlNodeTmp->text))); } else { xmlNodeTmp->get_childNodes(&subSubElementNodes); subSubElementNodes->get_length(&subLength); for (int j = 0;j < subLength;j++) { ExternalObject *objSub = new ExternalObject(); subSubElementNodes->get_item(j, &xmlNodeTmp); readObject(objSub, xmlNodeTmp); objRet->children.push_back(objSub); } } } return TRUE; }
// 获取当前节点的指定位置的Element子节点 IBpXMLNode *CBpXMLNode::GetElementChildByIndex(int nIndex) const { if (m_pDOMNode == NULL) return NULL; if (m_pDOMNode->GetnodeType() != MSXML2::NODE_ELEMENT) return NULL; MSXML2::IXMLDOMNodeListPtr pNodeList = m_pDOMNode->GetchildNodes(); if (pNodeList == NULL) return NULL; long nListLen = 0; pNodeList->get_length(&nListLen); if (nIndex <= 0 || nIndex > nListLen) return NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; int nRealIndex = 0; int nCount = 0; for (int i = 0; i < nListLen; i++) { pNodeList->get_item(i, &pNode); if (pNode->GetnodeType() == MSXML2::NODE_ELEMENT) { nCount++; if (nCount == nIndex) { nRealIndex = i; break; } } } if (nRealIndex < 0 || nRealIndex >= nListLen) return NULL; pNodeList->get_item(nRealIndex, &pNode); if (pNode->GetnodeType() == MSXML2::NODE_ELEMENT) { CBpXMLNode *pNodeCreated = new CBpXMLNode(); pNodeCreated->m_pDOMNode = pNode; return (IBpXMLNode*)pNodeCreated; } return NULL; }
bstr_t CCMSDIntegrator::GetProperty(MSXML2::IXMLDOMNodePtr pNode,bstr_t PropName, bstr_t defaultVal, bstr_t NodeType) { MSXML2::IXMLDOMNodeListPtr properties = pNode->selectNodes(NodeType); for(int j=0; j< properties->length; j++) { MSXML2::IXMLDOMNodePtr pProp = NULL; properties->get_item(j, &pProp); MSXML2::IXMLDOMNodePtr pName = pProp->selectSingleNode(bstr_t(".//Name")); MSXML2::IXMLDOMNodePtr pValue = pProp->selectSingleNode(bstr_t(".//Value")); bstr_t propName = (pName!=NULL) ? pName->Gettext() : L"None"; if( propName == PropName) { return (pValue!=NULL) ? pValue->Gettext() : defaultVal; } } return defaultVal; }
// 获取当前结点,指定名称的第index个Element子结点 IBpXMLNode *CBpXMLNode::GetElementChildByName(const wchar_t *szChildNodeName, int nIndex) const { if (m_pDOMNode == NULL || nIndex <= 0) return NULL; int nCount = nIndex; if (m_pDOMNode->GetnodeType() == MSXML2::NODE_ELEMENT) { MSXML2::IXMLDOMNodeListPtr pNodeList = m_pDOMNode->GetchildNodes(); if (pNodeList == NULL) return NULL; long nListLen = 0; pNodeList->get_length(&nListLen);; int i = 0; MSXML2::IXMLDOMNodePtr pNode = NULL; for (;;) { pNodeList->get_item(i, &pNode); if (pNode == NULL) break; if (pNode->GetnodeType() == MSXML2::NODE_ELEMENT) { BSTR bstrText = NULL; m_pDOMNode->get_nodeName(&bstrText); CString strTempNodeName(bstrText); if (strTempNodeName.CompareNoCase(szChildNodeName)) { nCount--; if(nCount == 0) { CBpXMLNode *pNodeCreated = new CBpXMLNode(); pNodeCreated->m_pDOMNode = pNode; return (IBpXMLNode*)pNodeCreated; } } } i++; if(i == nListLen) break; } } return NULL; }
//////////////////////////////////////////////////////////////////////////// // Function: Walk all the Elements in a loaded XML document. //////////////////////////////////////////////////////////////////////////// HRESULT CXMLDocument::WalkTree(CString& strSearchPattern) { HRESULT hr = S_OK; if (strSearchPattern.IsEmpty()) { MSXML2::IXMLDOMNodePtr pNode = m_pDoc; if (pNode == NULL) return E_FAIL; hr = WalkTree(0/*iLevel*/, pNode); } else { //j ("//video"); //j ("//video[@id='crap1']"); //j ("//video[@id='crap1']"); // filter the nodes using the search pattern MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; hr = m_pDoc->selectNodes(CComBSTR(strSearchPattern), &pNodeList); hr = CheckHR(hr, "in the search pattern"); // get the length of the resulting node list if (pNodeList) { long lLength; hr = pNodeList->get_length(&lLength); hr = CheckHR(hr, "retrieving node list length"); MSXML2::IXMLDOMNodePtr pNode = NULL; for (long i = 0; i < lLength; i++) { hr = pNodeList->get_item(i, &pNode); hr = WalkTree(0/*iLevel*/, pNode); if (hr != S_OK) return hr; } } } return hr; }
// 获取当前节点的所有Element子节点个数 int CBpXMLNode::GetElementCount() const { if (m_pDOMNode == NULL) return 0; long nResult = 0; MSXML2::IXMLDOMNodeListPtr pNodeList = m_pDOMNode->GetchildNodes(); if (pNodeList != NULL) { MSXML2::IXMLDOMNodePtr pNode = NULL; long nListLen = 0; pNodeList->get_length(&nListLen); for (long i = 0; i < nListLen; i++) { pNodeList->get_item(i, &pNode); if (pNode != NULL && pNode->GetnodeType() == MSXML2::NODE_ELEMENT) nResult++; } } return (int)nResult; }
// 判断当前节点是否存在Element子结点 bool CBpXMLNode::HasElementChildNodes() const { if(m_pDOMNode == NULL) return false; if (m_pDOMNode->GetnodeType() == MSXML2::NODE_ELEMENT) { MSXML2::IXMLDOMNodeListPtr pNodeList = m_pDOMNode->GetchildNodes(); if (pNodeList == NULL) return false; long nListLen = 0; pNodeList->get_length(&nListLen); MSXML2::IXMLDOMNodePtr pNode = NULL; for (int i = 0; i < nListLen; i++) { pNodeList->get_item(i, &pNode); if (pNode->GetnodeType() == MSXML2::NODE_ELEMENT) return true; } } return false; }
//通过WebService查询在巡防的终端信息,当终端发来GPS数据时,用来判断该终端是否在巡防, //如果是还要将该GPS数据发给巡防管理服务 BOOL CDBManage_WebServer::WebServer_LoadDB_PatrolDeviceInfo(BOOL isUpdateOnLineDeviceList) { BOOL bRet = FALSE; if (!gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.IsEmpty()) { gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.ClearAll(); } if(m_bWebServiceConn) { MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument)); if(FAILED(hr)) { gWriteLogApp.WriteLog( FOR_ALARM,_T("%s [SYS] 无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库\n"),CTime::GetCurrentTime().Format("%H:%M:%S")); return FALSE; } CComBSTR hiResult; m_pWebService->HYTPGIS_PatroAreaUser_QueryAllDevice(&hiResult); char *pStr = NULL; pStr = _com_util::ConvertBSTRToString(hiResult); CString strXML; strXML.Format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><DataSet xmlns=\"http://tempuri.org/\"> %s</DataSet>",pStr); VARIANT_BOOL bRet = pDoc->loadXML((_bstr_t)strXML); if(bRet == -1) { MSXML2::IXMLDOMNodeListPtr pListNode; pListNode = pDoc->selectNodes("//Table"); if(pListNode != NULL) { int i; long tLen; pListNode->get_length(&tLen); CString strTemp; for(i = 0 ; i < tLen; i++) { int j = 0; MSXML2::IXMLDOMNode *pTempNode; pListNode->get_item(i,&pTempNode); MSXML2::IXMLDOMNodeList *TempChildList; pTempNode->get_childNodes(&TempChildList); long listCount = 0; TempChildList->get_length(&listCount); MSXML2::IXMLDOMNode *tempListItem = NULL; _variant_t tempValue; for(j = 0 ; j < listCount; j++) { TempChildList->get_item(j,&tempListItem); tempListItem->get_nodeTypedValue(&tempValue); strTemp.Format("%s",(char *)(_bstr_t)tempValue); strTemp = strTemp.TrimLeft(); strTemp = strTemp.TrimRight(); gPGisMainCtrlProc.m_BusinessManage_ConnectToGPSCheckServer.m_PatrolDevListData.AddItem(strTemp); if(isUpdateOnLineDeviceList) //当巡防路线修改时为TRUE,此时要更新在线终端的巡防状态字段 { gPGisMainCtrlProc.m_BusinessManage_ConToPub.Update_OnlineDeviceList_PatrolState(strTemp); // } TRACE("添加巡防用户信息 %s \n",strTemp); } } //end for(i = 0 bRet = TRUE; gWriteLogApp.WriteLog( FOR_ALARM,_T("%s [SYS] 加载巡防终端信息 %d 成功 \n"),CTime::GetCurrentTime().Format("%H:%M:%S"),tLen); m_bWebConnOK = TRUE; } } if(pStr) { delete []pStr; pStr = NULL; } } return bRet; }
DataDictionary MTConnectStreamsParser::ParseDataItems() { MSXML2::IXMLDOMNodePtr root = m_pXMLDoc->GetdocumentElement(); MSXML2::IXMLDOMNodeListPtr nodes = root->selectNodes(_bstr_t("//DeviceStream")); DataDictionary data; try { for(int i=0; i< nodes->length; i++) { MSXML2::IXMLDOMNodePtr pNode = NULL; nodes->get_item(i, &pNode); _bstr_t items[3] = {_bstr_t(".//Samples"), _bstr_t(".//Events"), _bstr_t(".//Condition") }; for(int ii=0; ii<3 ; ii++) { MSXML2::IXMLDOMNodeListPtr samples = pNode->selectNodes(items[ii]); for(int j=0; j< samples->length; j++) { MSXML2::IXMLDOMNodePtr pSampleHive = NULL; samples->get_item(j, &pSampleHive); // Get each child MSXML2::IXMLDOMNodeListPtr childs = pSampleHive->childNodes; for(int k=0; k< childs->length; k++) { MSXML2::IXMLDOMNodePtr pSample = NULL; ptime datetime; std::string name ; std::string timestamp; std::string sequence; childs->get_item(k, &pSample); name = (LPCSTR) GetAttribute(pSample, "name"); if(name.empty()) name = (LPCSTR) GetAttribute(pSample, "dataItemId"); if(name.empty()) continue; timestamp = (LPCSTR) GetAttribute(pSample, "timestamp"); sequence = (LPCSTR) GetAttribute(pSample, "sequence"); // Lookup any name remapping to shorten if(TagRenames.find(name)!= TagRenames.end()) { name = TagRenames[name]; } if(items[ii] == _bstr_t(".//Samples")) data[name]=(LPCSTR) pSample->Gettext(); else if(items[ii] == _bstr_t(".//Events")) data[name]=(LPCSTR) pSample->Gettext(); else if(items[ii] == _bstr_t(".//Condition")) { std::string tagname = (LPCSTR) GetElement(pSample); if(stricmp(tagname.c_str(),"FAULT")==0) data[name]="fault"; else data[name]="normal"; } } } } } } catch(...) { std::cout<< "MTConnectStreamsParser::ParseDataItems() Exception\n"; } return data; }
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(); }