• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            C++中DOM寫XML

            用MSXML 4.0:
            1)用一般的指針
            2)用智能指針,比較簡(jiǎn)單

            下面的例子用智能指針:
             步驟:
                    

            Programmatically, the dynamDOMsmart application performs the following steps:

            1. Creates an XML DOM instance (pXMLDom).
            2. Calls the createProcessInstruction method on pXMLDom. This creates a processing instruction node (pi) targeted for XML 1.0.
            3. Calls the appendChild method on pXMLDom. This adds the processing instruction node (pi) to pXMLDom.
            4. Calls the createComment method on the DOM object (pXMLDom) to create a comment node (pc) and then append it pXMLDom.
            5. Creates a <root> element as the document element, with a created attribute whose value is set to a string value of "using DOM". Adds this element (<root>) to the DOM object (pXMLDom).
            6. Creates a <node1> element with some character data as its content. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
            7. Creates a <node2> element that contains a CDATA section (pcd) with markup text. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
            8. Creates a <node3> element that contains a DOM document fragment (pdf). This fragment contains three other empty child elements: <subNode1>, <subNode2>, and <subNode3>. The code then appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
            9. Saves this dynamically created DOM object to the project's main directory, and prints the XML data in the application console.
            源代碼:
            #include <stdio.h>
            #import 
            <msxml4.dll>
            using namespace MSXML2;
            int main(int argc, char* argv[])
            {
            IXMLDOMDocument3Ptr 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.
            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.
            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).
            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.
            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
            // we've 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("\n\t"));
            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("\n\t"));
            IXMLDOMCDATASectionPtr pcd;
            pcd 
            = pXMLDom->createCDATASection("<some mark-up text>");
            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("\n\t"));
            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("dynaDom.xml");
            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();
            return 0;
            }


            結(jié)果:
            Dynamically created DOM:
            <?xml version="1.0"?>
            <?xml-stylesheet type='text/xml' href='dom.xsl'?>
            <!--sample xml file created using XML DOM object.-->
            <root created="using dom">
            <node1>some character data</node1>
            <node2><![CDATA[<some mark-up text>]]></node2>
            <node3>
            <subNode1/>
            <subNode2/>
            <subNode3/>
            </node3>
            </root>
            DOM saved to dynamDom.xml
            -----------------------------
            在此過程中,經(jīng)常會(huì)有一些錯(cuò)誤:保存文件的路徑,有的時(shí)候我寫絕對(duì)路徑,但是結(jié)果它卻還是保存到相對(duì)路徑,(為什么那?)還有里面的字符格式的轉(zhuǎn)化,比較復(fù)雜,哈哈!歡迎大家來討論:

            哪位高手知道,關(guān)于保存路徑的具體的東西啊,反正我發(fā)現(xiàn)相對(duì)路徑有的時(shí)候并不總是相對(duì)你的原程序,當(dāng)你打開文件處理在保存時(shí),相對(duì)路徑是相對(duì)你程序打開的文件的路徑!



            還有其他的嗎,該輪到你們拉:


            posted on 2005-12-29 09:48 夢(mèng)在天涯 閱讀(6346) 評(píng)論(8)  編輯 收藏 引用 所屬分類: CPlusPlus 、UML/XML

            評(píng)論

            # re: C++中DOM寫XML 2005-12-29 13:44 夢(mèng)在天涯

            為什么createelement(item)時(shí),item不能是由數(shù)字轉(zhuǎn)換為的字符串!而且是有時(shí)可以有時(shí)不可,都發(fā)現(xiàn)好多次拉,難道時(shí)bug,還是英文版的緣故???  回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2005-12-29 14:53 小明

            我來回答你

            1.路徑的問題
            相對(duì)路徑都是相對(duì)于當(dāng)前的path,可能是可執(zhí)行程序所在路徑,跟源程序的路徑無關(guān),這對(duì)于任何win32的程序都一樣

            2.CreateElement為什么有時(shí)不能是數(shù)字?
            因?yàn)閤ml的節(jié)點(diǎn)有些不能是數(shù)字
            比如
            <root>
            <2>test</2>
            </root>
            不是有效的xml

            <root>
            <test>2</test>
            </root>
            就是有效的
              回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2005-12-29 16:09 夢(mèng)在天涯

            恩,太感謝拉!牛!

            為什么說有些節(jié)點(diǎn)不能是數(shù)字那?是所有的嗎?好像有的行??!^_^!


            哦,想起來拉,節(jié)點(diǎn)的命名首字母不能是()。。。。。。。^_^!~

            但是element里的text,我們不用管它是中文還是英文,也不用管是身編碼嗎?只要我們?cè)趚ml頭指定encoding=“”就可以了嗎?為什么有的時(shí)候加了encoding =“gb2312”,顯示仍然是亂麻那~???


            謝謝!  回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2006-01-20 13:50 flying

            我的編譯通不過啊!!!
            把IXMLDOMDocument3Ptr 改為IXMLDOMDocumentPtr能通過???
            上面的例子就是SDK里面的吧!  回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2006-01-20 15:22 flying

            請(qǐng)助!
            上面的代碼編譯不過,提示說IXMLDOMDocument3Ptr沒有定義!

            在system32中沒有msxml4a.dll ,而msxml4.dll 和 msxml4r.dll有.
            請(qǐng)問:是因?yàn)槿蹦莻€(gè)DLL造成編譯通不過的嗎?如果是,這么解決啊!

            懇求大俠幫助!!!!!!  回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2006-01-23 17:01 夢(mèng)在天涯

            是啊,要看你裝的MSXML的版本啊,有的要用3,有的不用啊!
            好像是這樣啊!  回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2006-02-08 20:50 夢(mèng)在天涯

            原程序?qū)τ行C(jī)器不能通過的原因:


            應(yīng)該把IXMLDOMDocument3Ptr中的3改為2!

            還有雖然用了namespace MSXML2,但是還必須在每個(gè)用到的定義如IXMLDOMElementPtr 前加上MSXML2::,這樣便可以通過!



            有知道為什么的嗎?


              回復(fù)  更多評(píng)論   

            # re: C++中DOM寫XML 2007-12-27 20:35 chenfeifei

            怎么用C++解析xml文件呢?需要對(duì)xml文件提取每個(gè)行字符 請(qǐng)求幫助。謝謝
            QQ:313054332  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1808116
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久精品国产2020| 久久久久久噜噜精品免费直播 | A狠狠久久蜜臀婷色中文网| 国产精品热久久无码av| 欧美伊人久久大香线蕉综合| 久久午夜羞羞影院免费观看| 久久av高潮av无码av喷吹| 无码专区久久综合久中文字幕| 狠狠色婷婷久久一区二区| 久久99国产精品久久| 国内精品九九久久精品| 久久精品国产72国产精福利| 99久久免费国产特黄| 久久亚洲AV成人无码| 精品久久人人做人人爽综合 | 国内精品九九久久精品| 99re久久精品国产首页2020| 久久天天婷婷五月俺也去| 色综合色天天久久婷婷基地| 久久综合给合久久狠狠狠97色69| 麻豆久久| 国产综合精品久久亚洲| 国产一久久香蕉国产线看观看| 亚洲国产精品嫩草影院久久| 2021久久精品国产99国产精品| 亚洲精品无码专区久久同性男| 久久国产精品成人免费| 午夜天堂精品久久久久| 亚洲欧美伊人久久综合一区二区 | 欧美精品丝袜久久久中文字幕| 国内精品久久久久| 丰满少妇高潮惨叫久久久| 久久久久免费视频| 欧美麻豆久久久久久中文| 亚洲国产精品嫩草影院久久| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久不射电影网| 久久99国产精品一区二区| 无码人妻少妇久久中文字幕蜜桃 | 国产成人久久AV免费| 人妻无码中文久久久久专区|