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; }
// Helper function to create a DOM instance: MSXML2::IXMLDOMDocument * DomFromCOM() { HRESULT hr; MSXML2::IXMLDOMDocument *pxmldoc = NULL; HRCALL( CoCreateInstance(__uuidof(DOMDocument), NULL, CLSCTX_INPROC_SERVER, __uuidof(MSXML2::IXMLDOMDocument), (void**)&pxmldoc), "Create a new DOMDocument"); HRCALL( pxmldoc->put_async(VARIANT_FALSE), "should never fail"); HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE), "should never fail"); HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE), "should never fail"); return pxmldoc; clean: if (pxmldoc) { pxmldoc->Release(); } return NULL; }
/* Helper function to create a DOM instance. */ MSXML2::IXMLDOMDocument *NXMLSettings::DomFromCOM() { HRESULT hr; MSXML2::IXMLDOMDocument *pxmldoc = NULL; hr = CoCreateInstance(__uuidof(MSXML2::DOMDocument30),NULL,CLSCTX_INPROC_SERVER, __uuidof(MSXML2::IXMLDOMDocument),reinterpret_cast<LPVOID *>(&pxmldoc)); if(SUCCEEDED(hr)) { pxmldoc->put_async(VARIANT_FALSE); pxmldoc->put_validateOnParse(VARIANT_FALSE); pxmldoc->put_resolveExternals(VARIANT_FALSE); pxmldoc->put_preserveWhiteSpace(VARIANT_TRUE); } return pxmldoc; }
TCHAR * CTaskModel::toXMLString(StringBuffer &sb, bool bKids) { BSTR str = 0; if(bKids) { HRESULT hr = m_pNode->get_xml(&str); if(hr == S_OK) { sb.append(str); SysFreeString(str); } } else { //TODO decide best code StringBuffer sbXslt(_T("<?xml version=\"1.0\" ?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"text\" /><xsl:variable name=\"item\" select=\"//task[@taskid='")); StringBuffer buf(32); sbXslt.append(ModelUtils::toHexString(m_id, buf)); sbXslt.append(_T("']\" /><xsl:template match=\"/\"><task <xsl:for-each select=\"$item/@*\"><xsl:value-of select=\"name()\" />="<xsl:value-of select=\".\" />" </xsl:for-each>><xsl:for-each select=\"$item/*[not(task)]\"><<xsl:value-of select=\"name()\" /> <xsl:for-each select=\"@*\"><xsl:value-of select=\"name()\" />="<xsl:value-of select=\".\" />" </xsl:for-each>><xsl:value-of select=\".\" /></<xsl:value-of select=\"name()\" />></xsl:for-each></task></xsl:template></xsl:stylesheet>")); MSXML2::IXMLDOMDocument *pDoc = 0; m_pNode->get_ownerDocument(&pDoc); BSTR xslt = SysAllocString(sbXslt); XMLTransform xform(xslt); HRESULT hr = pDoc->transformNode(xform.m_pStyleSheet, &str); //HRESULT hr = m_pNode->transformNode(xform.m_pStyleSheet, &str); if(hr == S_OK && str) { sb.append(str); SysFreeString(str); } else { DebugBreak(); } SysFreeString(xslt); pDoc->Release(); xslt = 0; } return sb; }
/* * Sends XML to the XMPP server. The XML is stored in an MSXML IXMLDOMDocument * object. */ STDMETHODIMP XMPP::SendXML(IDispatch* pDisp) { MSXML2::IXMLDOMDocument *pXMLDoc = NULL; BSTR xmlString; if(pDisp != NULL && SUCCEEDED(pDisp->QueryInterface(IID_IXMLDOMDocument, (LPVOID*) &pXMLDoc)) && SUCCEEDED(pXMLDoc->get_xml(&xmlString))) { m_ConnectionManager.SendText(xmlString); ::SysFreeString(xmlString); pXMLDoc->Release(); } else { return E_FAIL; } return S_OK; }
// 根据给定的名字,创建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; }
LRESULT CSCProgramBlacklistDlg::OnApplyMessage(WPARAM wParam, LPARAM lParam) { MSXML2::IXMLDOMDocument *pXmlDoc = reinterpret_cast<MSXML2::IXMLDOMDocument *>(wParam); ASSERT(pXmlDoc); MSXML2::IXMLDOMElement *pRootElem = reinterpret_cast<MSXML2::IXMLDOMElement *>(lParam); ASSERT(pRootElem); MSXML2::IXMLDOMElementPtr ProccessList = pXmlDoc->createElement(_T("ProccessList")); pRootElem->appendChild(ProccessList); for (int i = 0; i < m_list.GetItemCount(); i++) { MSXML2::IXMLDOMElementPtr ProccessInfo = pXmlDoc->createElement(_T("ProccessInfo")); ProccessList->appendChild(ProccessInfo); ProccessInfo->setAttribute(_T("Name"), (LPCTSTR)m_list.GetItemText(i, 1)); ProccessInfo->setAttribute(_T("AddTime"), (LPCTSTR)m_list.GetItemText(i, 0)); ProccessInfo->setAttribute(_T("Description"), (LPCTSTR)m_list.GetItemText(i, 2)); } return TRUE; }