HRESULT XMLHelper::CreateChild(MSXML2::IXMLDOMNode *pNode, MSXML2::IXMLDOMNode **ppChildNode, const TCHAR *pChildName, const TCHAR *pChildText) { *ppChildNode = 0; MSXML2::IXMLDOMDocument *pDoc = 0; HRESULT hr = pNode->get_ownerDocument(&pDoc); if(hr == S_OK) { VARIANTARG varg; VariantInit(&varg); varg.vt = VT_I2; varg.iVal = MSXML2::NODE_ELEMENT; BSTR name = SysAllocString(pChildName); hr = pDoc->createNode(varg, name, EMPTY_STR, ppChildNode); SysFreeString(name); if(*ppChildNode) { if(pChildText) { BSTR text = SysAllocString(pChildText); hr = (*ppChildNode)->put_text(text); SysFreeString(text); } hr = pNode->appendChild(*ppChildNode, ppChildNode); } } return hr; }
// 根据给定的名字,创建Element节点,并将其附加为当前节点的子节点 IBpXMLNode* CBpXMLNode::AppendNewElementChild(const wchar_t *szElementName) { if (m_pDOMNode == NULL) return NULL; MSXML2::IXMLDOMDocument *pDOMDocPtr = NULL; m_pDOMNode->get_ownerDocument(&pDOMDocPtr); if (pDOMDocPtr == NULL) return NULL; // 创建Element VARIANT vtTemp; vtTemp.vt = VT_I2; vtTemp.iVal = 1; MSXML2::IXMLDOMNodePtr pDOMNodeCreated = pDOMDocPtr->createNode(vtTemp, _bstr_t(szElementName), L""); if (pDOMNodeCreated == NULL) return NULL; // 用来指向被附加后的节点 MSXML2::IXMLDOMNodePtr pNodeAppend = NULL; try { pNodeAppend = m_pDOMNode->appendChild(pDOMNodeCreated); } catch (...) { return NULL; } if (pNodeAppend == NULL) { pDOMNodeCreated->Release(); return NULL; } CBpXMLNode* pElemCreated = new CBpXMLNode; pElemCreated->m_pDOMNode = pNodeAppend; return pElemCreated; }