MFC创建XML文件示例代码
发布时间:2020-12-16 08:05:05 所属栏目:百科 来源:网络整理
导读:void CreateXml(){ CoInitialize(NULL); // 创建文档 MSXML2::IXMLDOMDocument2Ptr pXMLDoc = NULL; //创建DOMDocument对象 HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument)); if (!SUCCEEDED(hr)) { return; } // Create the Processin
void CreateXml() { CoInitialize(NULL); // 创建文档 MSXML2::IXMLDOMDocument2Ptr pXMLDoc = NULL; //创建DOMDocument对象 HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument)); if (!SUCCEEDED(hr)) { return; } // Create the Processing Instruction MSXML2::IXMLDOMProcessingInstructionPtr pXMLProc = NULL; pXMLProc = pXMLDoc->createProcessingInstruction("xml","version='1.0' encoding='UTF-8'"); _variant_t vNullVal; vNullVal.vt = VT_NULL; pXMLDoc->insertBefore(pXMLProc,vNullVal); // 创建根结点 _variant_t varNodeType((short)MSXML2::NODE_ELEMENT); MSXML2::IXMLDOMNodePtr pXMLNodeRoot= NULL; pXMLNodeRoot = pXMLDoc->createNode(varNodeType,_T("Cases"),_T("")); // 添加根结点 pXMLDoc->appendChild(pXMLNodeRoot); // 创建并添加下级结点 MSXML2::IXMLDOMNodePtr pXMLNodeNode= NULL; pXMLNodeNode = pXMLNodeRoot->appendChild(pXMLDoc->createElement(_T("Case"))); // 创建下级元素结点 MSXML2::IXMLDOMElementPtr pXMLEle = NULL; pXMLEle = pXMLDoc->createElement(_T("CopyFile")); // 创建并设置下级结点属性 MSXML2::IXMLDOMAttributePtr pXMLAttr = NULL; pXMLAttr = pXMLDoc->createAttribute(_T("src")); pXMLAttr->nodeTypedValue = "C:test.txt"; pXMLEle->attributes->setNamedItem(pXMLAttr); pXMLAttr = pXMLDoc->createAttribute(_T("dest")); pXMLAttr->nodeTypedValue = "D:Test.txt"; pXMLEle->attributes->setNamedItem(pXMLAttr); // 添加元素结点 pXMLNodeNode->appendChild( pXMLEle); MSXML2::IXMLDOMElementPtr pXMLEle1 = NULL; pXMLEle1 = pXMLDoc->createElement(_T("DelFile")); pXMLEle1->appendChild(pXMLDoc->createTextNode("C:test.txt")); // 添加元素结点 pXMLNodeNode->appendChild( pXMLEle1); // 保存文档 pXMLDoc->save(_T("F:Test.xml")); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |