// 获取当前节点的最后一个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; }
// 获取当前节点的指定位置的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; }
// 获取当前结点,指定名称的第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; }
// 获取当前节点的所有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; }