MSXML2::IXMLDOMNodePtr CXmlBase::InsertEnterAndTabs(MSXML2::IXMLDOMNodePtr pParentNode, MSXML2::IXMLDOMNodePtr pNode, int nLevel) { HRESULT hr = S_OK; MSXML2::IXMLDOMDocument2Ptr pDoc = NULL; MSXML2::IXMLDOMTextPtr pTabsText = NULL; MSXML2::IXMLDOMNodePtr pTabsNode = NULL; MSXML2::IXMLDOMElementPtr pElement = NULL; CString strText(_T("")), strTabs(_T("")); CComBSTR bstrText; if (nLevel < 0 || pParentNode == NULL) return NULL; if(pNode == NULL)nLevel = 0; for (int i=0; i<nLevel; i++) { strTabs += _T("\t"); } strText.Format(_T("%s%s"), _T("\r\n"), (LPCTSTR)strTabs); bstrText = _bstr_t(strText).GetBSTR(); try { pDoc = pParentNode->GetownerDocument(); if (pDoc == NULL) return NULL; pTabsText = pDoc->createTextNode((LPCTSTR)strText); if (pTabsText == NULL) return NULL; CComVariant varNode = pNode.GetInterfacePtr(); pTabsNode = pParentNode->insertBefore(pTabsText, varNode); //hr = pDoc->raw_createTextNode(bstrText.m_str, &pTabsText); //if (FAILED(hr) || pTabsText == NULL) return NULL; //pElement = pTabsText; //pTabsNode = pParentNode->appendChild(pElement); //pTabsNode = pDoc->createTextNode((LPCTSTR)strText); //if (pTabsNode == NULL) return NULL; //pNode = pNode->appendChild(pTabsNode); } catch(_com_error &e) { CString str; str.Format(_T("创建xml节点发生异常。\r\nComError:\r\n%s") , e.ErrorMessage()); ::MessageBox(NULL,str, g_sAppTitle, MB_OK|MB_ICONERROR); return NULL; } catch(...) { return NULL; } return pTabsNode; }
MSXML2::IXMLDOMNode* CXmlNodeWrapper::InsertAfter(MSXML2::IXMLDOMNode *refNode, MSXML2::IXMLDOMNode *pNode) { MSXML2::IXMLDOMNodePtr nextNode = refNode->GetnextSibling(); MSXML2::IXMLDOMNodePtr newNode; if (nextNode.GetInterfacePtr() != NULL) newNode = m_xmlnode->insertBefore(pNode,(IUnknown*)nextNode); else newNode = m_xmlnode->appendChild(pNode); return newNode.Detach(); }
MSXML2::IXMLDOMNode* CXmlNodeWrapper::InsertAfter(MSXML2::IXMLDOMNode *refNode, LPCTSTR nodeName) { MSXML2::IXMLDOMDocumentPtr xmlDocument = m_xmlnode->GetownerDocument(); if (xmlDocument) { MSXML2::IXMLDOMNodePtr newNode = xmlDocument->createNode(_variant_t((short)MSXML2::NODE_ELEMENT),nodeName,""); MSXML2::IXMLDOMNodePtr nextNode = refNode->GetnextSibling(); if (nextNode.GetInterfacePtr() != NULL) newNode = m_xmlnode->insertBefore(newNode.Detach(),(IUnknown*)nextNode); else newNode = m_xmlnode->appendChild(newNode.Detach()); return newNode.Detach(); } return NULL; }
MSXML2::IXMLDOMNodePtr CXmlBase::InsertNode( MSXML2::IXMLDOMNodePtr pParentNode , MSXML2::IXMLDOMNodePtr pNodeBack , LPCTSTR lpszNodeText ) { HRESULT hr = S_OK; MSXML2::IXMLDOMDocument2Ptr pDoc = NULL; MSXML2::IXMLDOMElementPtr pElement = NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; CComBSTR bstrNodeText = _bstr_t("node").GetBSTR(); if (pParentNode == NULL || lpszNodeText == NULL) return NULL; bstrNodeText = lpszNodeText; if (bstrNodeText.Length() == 0) return NULL; try { pDoc = pParentNode->GetownerDocument(); if (pDoc == NULL) return NULL; //hr = pDoc->raw_createElement(bstrNodeText.m_str, &pElement); //if (FAILED(hr) || pElement == NULL) return NULL; pNode = pDoc->createNode(NODE_ELEMENT, bstrNodeText.m_str, _T("")); if (pNode == NULL) return NULL; CComVariant varNode = pNodeBack.GetInterfacePtr(); pNode = pParentNode->insertBefore(pNode, varNode); } catch(_com_error &e) { CString str; str.Format(_T("插入xml节点发生异常。\r\nComError:\r\n%s") , e.ErrorMessage()); ::MessageBox(NULL,str, g_sAppTitle, MB_OK|MB_ICONERROR); return NULL; } catch(...) { return NULL; } return pNode; }