QT读写XML

前端之家收集整理的这篇文章主要介绍了QT读写XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
test.xml
@H_502_4@
Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <Startaniminal="cat">
  3. book>book1</Start>


simpleWriter.cpp
@H_502_4@
C++代码
    #include<QXmlStreamWriter>
  1. #include<QFile>
  2. #include<iostream>
  3. usingnamespacestd;
  4. voidwriteXML(){
  5. QFilefile("test.xml");
  6. if(notfile.open(QIODevice::ReadWrite))
  7. return;
  8. QXmlStreamWriterwriter(&file);
  9. writer.setAutoFormatting(true);
  10. writer.writeStartDocument();
  11. writer.writeStartElement("Start");
  12. writer.writeAttribute("animinal","cat");
  13. writer.writeTextElement("book","book1");
  14. writer.writeEndElement();//Start
  15. writer.writeEndDocument();
  16. file.close();
  17. }
  18. intmain(){
  19. writeXML();
  20. }

dom读写
dom.xml
dom.cpp
#include<QDomDocument>
  • #include<QDomElement>
  • #include<QTextStream>
  • voidwriteDom(){
  • QFilefile("dom.xml");
  • QDomDocumentdoc;
  • QDomElementstartElem=doc.createElement("Start");
  • doc.appendChild(startElem);
  • startElem.setAttribute("animinal",250); line-height:18px"> QDomElementbookElem=doc.createElement("book");
  • bookElem.appendChild(doc.createTextNode("book1"));
  • startElem.appendChild(bookElem);
  • QTextStreamout(&file);
  • doc.save(out,3);
  • voidreadDom(){
  • doc.setContent(&file);
  • QDomElementstartElem=doc.documentElement();
  • cout<<"animainal:";
  • cout<<startElem.attribute("animinal").toStdString()<<endl;
  • cout<<"book:";
  • cout<<startElem.firstChildElement("book").text().toStdString()<<endl;
  • intmain()
  • {
  • writeDom();
  • readDom();
  • animainal:cat
  • book:book1
  • 原文链接:https://www.f2er.com/xml/298046.html

    猜你在找的XML相关文章