• <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>
            posts - 10,  comments - 2,  trackbacks - 0
            QDomDocument dom;
            QFile 
            *file = new QFile("books.xml");
            if (file->open(QIODevice::ReadOnly))
            {
                dom.setContent(file); 
            // 此處需做錯(cuò)誤判斷
            }
            file
            ->close();

            // 以下代碼為解析XML文件中的內(nèi)容
            QDomNodeList books = dom.elementsByTagName("book");
            for (int i = 0; i < books.count(); i++) {
                QDomNode book 
            = books.item(i);
                
            if (book.toElement().attribute("ISBN"== isbn) { // 查找ISBN匹配的屬性,isbn由用戶指定
                    QDomNodeList attribs = book.toElement().childNodes();

                    QDomNode node;
                    QString attribNumber,attribContent;
                    
            for (int j = 0; j < attribs.count(); j++) {
                        node 
            = attribs.item(j);
                        attribNumber 
            = node.toElement().attribute("number");
                        attribContent 
            = node.toElement().text();
                        
            //  在此可添加代碼對(duì)attribNumber,attribContent作出處理了.
                    }
                    
            break;
                }
            }

            // 以下代碼為寫(xiě)內(nèi)容進(jìn)XML文件
            QStringList attribs = ""// 由用戶指定
            QDomElement bookNode = dom.createElement("book");
            carNode.setAttribute(
            "ISBN", isbn); // isbn由用戶指定

            //此處開(kāi)始處理attribs字符串列表中的內(nèi)容
            for (int i = 0; i < attribs.count(); i++) {
                QString attribNumber 
            = QString::number(i+1); // 內(nèi)容的序號(hào): 01 02 
                if (i < 10)
                    attribNumber.prepend(
            "0");

                QDomText textNode 
            = dom.createTextNode(attribs.at(i)); // 取出每一個(gè)字符串

                QDomElement attribNode 
            = dom.createElement("attrib");
                attribNode.setAttribute(
            "number", attribNumber);
                attribNode.appendChild(textNode);

                carNode.appendChild(attribNode); 
            // 屬性結(jié)束
            }

            QDomNodeList archive 
            = carDetails.elementsByTagName("archive");
            archive.item(
            0).appendChild(carNode);

            // 寫(xiě)入XML文件中
            if (file->open(QIODevice::WriteOnly)) {
                QTextStream stream(file);
                archive.item(
            0).save(stream, 4);
                file
            ->close();
            }

            <?xml version="1.0" encoding="gb2312" ?>
            <archive>
                    
            <book ISBN="9787111267423">
                        
            <attrib number="01">書(shū)名:C++模板元編程(C++ 領(lǐng)域扛鼎之作,榮耀先生傾情翻譯,名著名譯)</attrib>
                        
            <attrib number="02">價(jià)格:55元</attrib>
                        
            <attrib number="03">作者:(美)David Abrahams;Aleksey Gurtovoy</attrib>
                        
            <attrib number="04">出版社:機(jī)械工業(yè)出版社</attrib>
                        
            <attrib number="05">出版日期:2010-01-01</attrib>
                    
            </book>
            </archive>
            posted on 2011-11-14 10:22 再生的雄鷹 閱讀(9222) 評(píng)論(1)  編輯 收藏 引用

            FeedBack:
            # re: [原創(chuàng)]Qt下XML文檔的讀、寫(xiě)(DOM方式)
            2011-11-15 21:50 | ccsdu2009
            看見(jiàn)這個(gè)都煩
            為了解析一個(gè)xml寫(xiě)這么多太費(fèi)時(shí)間了
            還不如弄一個(gè)自動(dòng)串行化的對(duì)象
              回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理



            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(1)

            隨筆檔案

            文章分類(lèi)

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产99久久久国产精免费| 99久久免费只有精品国产| 久久久人妻精品无码一区| 久久久久亚洲AV综合波多野结衣| 狠狠精品干练久久久无码中文字幕| 久久国产综合精品五月天| 精品久久久久久国产| 99久久久精品| 久久久久亚洲AV成人网人人网站| 午夜久久久久久禁播电影| 久久精品成人一区二区三区| 亚洲伊人久久精品影院| 丁香五月网久久综合| 久久久国产打桩机| 国产日韩欧美久久| 久久久久久久亚洲Av无码| 久久久免费观成人影院| 久久九九亚洲精品| 亚洲av成人无码久久精品| 久久久噜噜噜久久| 久久青青草原综合伊人| 久久久无码精品亚洲日韩按摩 | 熟妇人妻久久中文字幕| 久久亚洲2019中文字幕| 国产巨作麻豆欧美亚洲综合久久 | 亚洲一级Av无码毛片久久精品| 精品久久久久中文字幕日本| 久久久久青草线蕉综合超碰| 久久精品成人影院| 久久久久国产亚洲AV麻豆| 欧美亚洲另类久久综合| 亚洲成人精品久久| 2021国产成人精品久久| 亚洲乱亚洲乱淫久久| 免费观看久久精彩视频| 国产亚州精品女人久久久久久| 国产精品久久久久天天影视| 91久久婷婷国产综合精品青草| 亚洲欧美日韩中文久久| 日韩人妻无码一区二区三区久久 | 久久综合亚洲色HEZYO国产|