void CCVideoDlg::CreateXML() { char saveXMLReportName[32]=”xmlReport.xml”; MSXML2::IXMLDOMDocument2Ptr pXMLDom; MSXML2::IXMLDOMProcessingInstructionPtr pi; HRESULT hr; CoInitialize(NULL); hr = pXMLDom.CreateInstance(__uuidof(DOMDocument)); if (Failed(hr)) { printf("Failed to CreateInstance on an XML DOM"); } pXMLDom->preserveWhiteSpace = VARIANT_TRUE; //添加声明 pi = pXMLDom->createProcessingInstruction("xml","version='1.0'"); if (pi != NULL) { pXMLDom->appendChild(pi); pi.Release(); } //创建样式表 pi = pXMLDom->createProcessingInstruction("xml-stylesheet","type='text/xsl' href='./../utils/detail.xsl'"); if (pi != NULL) { pXMLDom->appendChild(pi); pi.Release(); } // 创建注释 MSXML2::IXMLDOMCommentPtr pc; pc = pXMLDom->createComment("xmlReport.xml created using XML DOM object."); if (pc != NULL) { pXMLDom->appendChild(pc); pc.Release(); } // 创建root节点 MSXML2::IXMLDOMElementPtr pe; pe = pXMLDom->createElement("root"); //创建root节点属性 MSXML2::IXMLDOMAttributePtr pa; pa = pXMLDom->createAttribute("rootAttribute"); if (pa != NULL) { pa->value = "value"; pe->setAttributeNode(pa); pa.Release(); } // 添加root节点 pXMLDom->appendChild(pe); //释放root节点 pe.Release(); //创建新的节点 text node pe = pXMLDom->createElement("textNode"); if (pe != NULL) { //换行 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t")); pe->text = "带有text的node"; pXMLDom->documentElement->appendChild(pe); pe.Release(); } //创建新的注释节点. pe=pXMLDom->createElement("markupNode"); if (pe != NULL) { // 换行 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t")); MSXML2::IXMLDOMCDATASectionPtr pcd; pcd = pXMLDom->createCDATASection("<some mark-up text>"); if (pcd != NULL) { pe->appendChild(pcd); pcd.Release(); } pXMLDom->documentElement->appendChild(pe); pe.Release(); } // 创建有属性节点 pe=pXMLDom->createElement("AttributenNode"); if (pe != NULL) { // 换行 pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t")); MSXML2::IXMLDOMDocumentFragmentPtr pdf; pdf = pXMLDom->createDocumentFragment(); pdf->appendChild(pXMLDom->createTextNode("\n\t\t")); pdf->appendChild(pXMLDom->createElement("subNode1")); pdf->appendChild(pXMLDom->createTextNode("\n\t\t")); pdf->appendChild(pXMLDom->createElement("subNode2")); pdf->appendChild(pXMLDom->createTextNode("\n\t\t")); pdf->appendChild(pXMLDom->createElement("subNode3")); pdf->appendChild(pXMLDom->createTextNode("\n\t")); pe->appendChild(pdf); pdf.Release(); pXMLDom->documentElement->appendChild(pe); pe.Release(); pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n")); } printf("Dynamically created DOM:\n%s\n",(LPCSTR)pXMLDom->xml); hr = pXMLDom->save((_variant_t)saveXMLReportName); if (Failed(hr)) { printf("Failed to save DOM to dynaDom.xml\n"); } else { printf("DOM saved to dynamDom.xml\n"); } if (pXMLDom) pXMLDom.Release(); CoUninitialize(); }
效果图