资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
VS2008 VC+ XML 操作1. msxmlchs.msi 的安装安装 msxml 4.0 sp2 时要注意不要用默认安装,要选择自定义安装(全部安装)才能选择安装 SDK,若没有选择安装 SDk 会发现安装完成后安装目录下什么也没有,而选择了 SDK的安装可以在目录下看到 LIB INC doc 3 个文件夹 2. 注册 dll命令行输入如下命令可以弹出注册成功的对话框cd c:windowssystem32regsvr32 msxml4.dll (也可在运行框中直接输入这句) 3. VS2008 设置工具-选项 -项目和解决方案 -VC+目录 Include 添加 INC 的的路径 C:Program FilesMSXML 4.0INCLibrary 添加 INC 的的路径 C:Program FilesMSXML 4.0LIB4. VC60 设置Tools-Options. -Directories tab-Include 添加 INC 的的路径 C:Program FilesMSXML 4.0INCTools-Options. -Directories tab-Library 添加 INC 的的路径 C:Program FilesMSXML 4.0LIB5. 新建项目,链接库代码中#include VC60 open Project Settings by clicking Settings from the Project menu. Select the Link tab. Append msxml2.lib to the Object/library modules: textbox.6. Code1:/部分变量无法识别时在前面加命名空间MSXML2: #include #import using namespace MSXML2;int main(int argc, char* argv)MSXML2:IXMLDOMDocument2Ptr pXMLDom;HRESULT hr;CoInitialize(NULL);hr = pXMLDom.CreateInstance(_uuidof(DOMDocument40);if (FAILED(hr)printf(Failed to CreateInstance on an XML DOM);return NULL;pXMLDom-preserveWhiteSpace = VARIANT_TRUE;/ Create a processing instruction targeted for xml.MSXML2:IXMLDOMProcessingInstructionPtr pi;pi = pXMLDom-createProcessingInstruction(xml, version=1.0);if (pi != NULL) pXMLDom-appendChild(pi);pi.Release();/ Create a processing instruction targeted for xml-stylesheet.pi = pXMLDom-createProcessingInstruction(xml-stylesheet,type=text/xml href=dom.xsl);if (pi != NULL) pXMLDom-appendChild(pi);pi.Release();/ Create a comment for the document.MSXML2:IXMLDOMCommentPtr pc;pc = pXMLDom-createComment(sample xml file created using XML DOM object.);if (pc != NULL) pXMLDom-appendChild(pc);pc.Release();/ Create the root element (i.e., the documentElement).MSXML2:IXMLDOMElementPtr pe;pe = pXMLDom-createElement(root);/ Create a created attribute for the root element and/ assign the using dom character data as the attribute value.MSXML2:IXMLDOMAttributePtr pa;pa = pXMLDom-createAttribute(created);if (pa != NULL)pa-value = using dom;pe-setAttributeNode(pa);pa.Release();/ Add the root element to the DOM instance.pXMLDom-appendChild(pe);pe.Release();/ Next, we will create and add more nodes to the root element/ weve just created./ Create an element to hold text content.pe = pXMLDom-createElement(node1);if (pe != NULL)/ Add newline + tab for indentation.pXMLDom-documentElement-appendChild(pXMLDom-createTextNode(nt);pe-text = some character data;pXMLDom-documentElement-appendChild(pe);pe.Release();/ Create an element to hold a CDATA section.pe=pXMLDom-createElement(node2);if (pe != NULL)/ Add newline + tab for indentation.pXMLDom-documentElement-appendChild(pXMLDom-createTextNode(nt);MSXML2:IXMLDOMCDATASectionPtr pcd;pcd = pXMLDom-createCDATASection();if (pcd != NULL) pe-appendChild(pcd);pcd.Release();pXMLDom-documentElement-appendChild(pe);pe.Release();/ Create an element to hold three empty subelements.pe=pXMLDom-createElement(node3);if (pe != NULL)/ Add newline +tab for indentation.pXMLDom-documentElement-appendChild(pXMLDom-createTextNode(nt);MSXML2:IXMLDOMDocumentFragmentPtr pdf;pdf = pXMLDom-createDocumentFragment();pdf-appendChild(pXMLDom-createTextNode(ntt);pdf-appendChild(pXMLDom-createElement(subNode1);pdf-appendChild(pXMLDom-createTextNode(ntt);pdf-appendChild(pXMLDom-createElement(subNode2);pdf-appendChild(pXMLDom-createTextNode(ntt);pdf-appendChild(pXMLDom-createElement(subNode3);pdf-appendChild(pXMLDom-createTextNode(nt);pe-appendChild(pdf);pdf.Release();pXMLDom-documentElement-appendChild(pe);pe.Release();pXMLDom-documentElement-appendChild(pXMLDom-createTextNode(n);printf(Dynamically created DOM:n%sn,(LPCSTR)pXMLDom-xml);hr = pXMLDom-save(dynaDom.xml);if (FAILED(hr)printf(Failed to save DOM to dynaDom.xmln);elseprintf(DOM saved to dynamDom.xmln);if (pXMLDom) pXMLDom.Release();CoUninitialize();return 0;结果:Dynamically created DOM:some character dataDOM saved to dynamDom.xml7. Code2(未编译验证): #include #include / You might need to add the msxml4/sdk/(inc, lib) directories / to the Tools-Options.-Directories in Visual Studio./ You might also need to append msxml2.lib to the/ Project-Settings.-Link-Object/Libray Modules field.int main(int argc, char* argv)HRESULT hr;IXMLDOMDocument3 *pXMLDoc = NULL;IXMLDOMParseError * pObjError = NULL;BSTR bstr = NULL;VARIANT_BOOL status;VARIANT vSrc;CoInitialize(NULL);hr = CoCreateInstance(CLSID_DOMDocument40, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument3,(void*)&pXMLDoc);if (FAILED(hr) pr
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号