用msxml写xml文件

写xml文件 1. //Create the XML MSXML::IXMLDOMDocument2Ptr pXMLDoc; HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument40)); Verify(pXMLDoc->loadXML(_T(" ")) == VARIANT_FALSE)));// 为xml最顶层的结点 2. //Get the root element just created MSXML::IXMLDOMElementPtr pXMLRootElem = pXMLDoc->GetdocumentElement(); //Add an attribute pXMLRootElem->setAttribute(_T("Depth"),_variant_t(_T("0"))); 3.//xml header MSXML::IXMLDOMProcessingInstructionPtr pXMLProcessingNode = pXMLDoc->createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'"); _variant_t vtObject; vtObject.vt = VT_DISPATCH; vtObject.pdispVal = pXMLRootElem; vtObject.pdispVal->AddRef(); pXMLDoc->insertBefore(pXMLProcessingNode,vtObject); 4.//Create the child elements and set the attributes MSXML::IXMLDOMElementPtr pXMLChild1 = pXMLDoc->createElement(_T("Child1")); //Create first child element pXMLChild1->setAttribute(_T("Depth"),_T("1")); pXMLChild1->Puttext(_T("This is a child of Parent")); //Set the element value pXMLChild1 = pXMLRootElem->appendChild(pXMLChild1); MSXML::IXMLDOMElementPtr pXMLChild2 = pXMLDoc->createElement(_T("Child2")); pXMLChild2->setAttribute(_T("Depth"), _T("1")); pXMLChild2 = pXMLRootElem->appendChild(pXMLChild2); //Child2 is a sibling of Child1 MSXML::IXMLDOMElementPtr pXMLChild3 = pXMLDoc->createElement(_T("Child3")); pXMLChild3->setAttribute(_T("Depth"), _T("2")); pXMLChild3 = pXMLChild2->appendChild(pXMLChild3); //Child3 is a direct child of Child2 MSXML::IXMLDOMElementPtr pXMLChild4 = pXMLDoc->createElement(_T("Child4")); pXMLChild4->setAttribute(_T("Depth"), _T("3")); pXMLChild4->Puttext(_T("This is a child of Child3")); pXMLChild4 = pXMLChild3->appendChild(pXMLChild4); //Child4 is a direct child of Child3 4.// Format the XML. This requires a style sheet MSXML::IXMLDOMDocument2Ptr loadXML; hr = loadXML.CreateInstance(__uuidof(DOMDocument40)); if(FAILED(hr)) { ShowError(loadXML); return; } //We need to load the style sheet which will be used to indent the XMl properly. if(loadXML->load(variant_t(_T("StyleSheet.xsl"))) == VARIANT_FALSE) { ShowError(loadXML); return; } //Create the final document which will be indented properly MSXML::IXMLDOMDocument2Ptr pXMLFormattedDoc; hr = pXMLFormattedDoc.CreateInstance(__uuidof(DOMDocument40)); CComPtr pDispatch; hr = pXMLFormattedDoc->QueryInterface(IID_IDispatch, (void**)&pDispatch); if(FAILED(hr)) { return; } _variant_t vtOutObject; vtOutObject.vt = VT_DISPATCH; vtOutObject.pdispVal = pDispatch; vtOutObject.pdispVal->AddRef(); //Apply the transformation to format the final document hr = pXMLDoc->transformNodeToObject(loadXML,vtOutObject); //By default it is writing the encoding = UTF-16. Let us change the encoding to UTF-8 MSXML::IXMLDOMNodePtr pXMLFirstChild = pXMLFormattedDoc->GetfirstChild(); // MSXML::IXMLDOMNamedNodeMapPtr pXMLAttributeMap = pXMLFirstChild->Getattributes(); // A map of the a attributes (vesrsion, encoding) values (1.0, UTF-8) pair MSXML::IXMLDOMNodePtr pXMLEncodNode = pXMLAttributeMap->getNamedItem(_T("encoding")); pXMLEncodNode->PutnodeValue(_T("UTF-8")); //encoding = UTF-8 5.//save if(sLocation.IsEmpty()) //User forgot to set the lcoation? sLocation = _T("Javed.xml"); hr = pXMLFormattedDoc->save(sLocation.AllocSysString());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值