我想要一个用vs2010写一个能生成xml文件的代码,简单一点的,求帮助! 5

 我来答

答题抽奖

首次认真答题后

即可获得3次抽奖机会,100%中奖。

更多问题

1条回答

#include <objbase.h>
#include <oleauto.h>
#include <msxml2.h>
#include <stdio.h>
 
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "msxml2.lib")
 
int main()
{
    if (CoInitialize(NULL) == S_OK) {
        IXMLDOMDocument* pXmlDoc;
        if (CoCreateInstance(CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (LPVOID*)&pXmlDoc) == S_OK) {
            pXmlDoc->put_async(0xffff);
             
            IXMLDOMElement* eBaidu;
            BSTR eBaiduName = SysAllocString(L"Baidu");
            pXmlDoc->createElement(eBaiduName, &eBaidu);
            SysFreeString(eBaiduName);
            pXmlDoc->putref_documentElement(eBaidu);
             
            IXMLDOMElement* eZhidao;
            BSTR eZhidaoName = SysAllocString(L"Zhidao");
            pXmlDoc->createElement(eZhidaoName, &eZhidao);
            SysFreeString(eZhidaoName);
            eBaidu->appendChild(eZhidao, NULL);
             
            IXMLDOMElement* eCPP;
            BSTR eCPPName = SysAllocString(L"CPP");
            pXmlDoc->createElement(eCPPName, &eCPP);
            SysFreeString(eCPPName);
            BSTR eCPPText = SysAllocString(L"C/C++ Questions");
            eCPP->put_text(eCPPText);
            SysFreeString(eCPPText);
            eZhidao->appendChild(eCPP, NULL);
 
            IXMLDOMElement* eJava;
            BSTR eJavaName = SysAllocString(L"Java");
            pXmlDoc->createElement(eJavaName, &eJava);
            SysFreeString(eJavaName);
            BSTR eJavaText = SysAllocString(L"Java Questions");
            eJava->put_text(eJavaText);
            SysFreeString(eJavaText);
            eZhidao->appendChild(eJava, NULL);
             
            IXMLDOMElement* eBaike;
            BSTR eBaikeName = SysAllocString(L"Baike");
            pXmlDoc->createElement(eBaikeName, &eBaike);
            SysFreeString(eBaikeName);
            eBaidu->appendChild(eBaike, NULL);
             
            eZhidao->Release();
            eBaike->Release();
 
            BSTR szXml;
            pXmlDoc->get_xml(&szXml);
            pXmlDoc->Release();
             
            eBaidu->Release();
             
            wprintf(L"%s", szXml);
            SysFreeString(szXml);
        } else {
            fprintf(stderr, "%s", "Can't create msxml instance");
        }
 
        CoUninitialize();
    }
 
    return 0;
}

调用系统里带的微软的那个xml库(com组件)生成xml的例子

会比较麻烦一点,不过好处是不需要单独安装额外的库啊什么的

空雪梦见 | 发布于2013-12-06
评论

为您推荐:

×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消