QT 读写XML

来源:互联网 发布:美橙互联 域名证书 编辑:程序博客网 时间:2024/06/09 19:54



  QDomDocument doc("mydocument");
  QFile file("mydocument.xml");
  if (!file.open(QIODevice::ReadOnly))
      return;
  if (!doc.setContent(&file)) {
      file.close();
      return;
  }
  file.close();


  // print out the element names of all elements that are direct children
  // of the outermost element.
  QDomElement docElem = doc.documentElement();


  QDomNode n = docElem.firstChild();
  while(!n.isNull()) {
      QDomElement e = n.toElement(); // try to convert the node to an element.
      if(!e.isNull()) {
          cout << qPrintable(e.tagName()) << endl; // the node really is an element.
      }
      n = n.nextSibling();
  }


  // Here we append a new element to the end of the document
  QDomElement elem = doc.createElement("img");
  elem.setAttribute("src", "myimage.png");
  docElem.appendChild(elem);


Once doc and elem go out of scope, the whole internal tree representing the XML document is deleted.
To create a document using DOM use code like this:


  QDomDocument doc("MyML");
  QDomElement root = doc.createElement("MyML");
  doc.appendChild(root);


  QDomElement tag = doc.createElement("Greeting");
  root.appendChild(tag);


  QDomText t = doc.createTextNode("Hello World");
  tag.appendChild(t);


  QString xml = doc.toString();







#include "readxml.h"

#include <QDomDocument>
#include <QFile>
#include <QTextStream>
#include <QDomAttr>




readXml::readXml(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);


connect(ui.pushButton, SIGNAL(clicked()), SLOT(OnBtn()));


connect(ui.pushButton_2, SIGNAL(clicked()), SLOT(OnReadBtn()));
}


void readXml::OnBtn()
{
QDomDocument doc;


QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");


doc.appendChild(instruction);


QDomElement root = doc.createElement("Notes");


doc.appendChild(root);




QDomElement note = doc.createElement("note");


root.appendChild(note);


QDomElement noteq = doc.createElement("noteq");


noteq.setAttribute("noteq", "qqqq");


root.appendChild(noteq);


QDomElement no = doc.createElement("no");


note.appendChild(no);


no.setAttribute("key","equipmentname");


QFile file("test.xml");


if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))


return ;


QTextStream out(&file);


out.setCodec("UTF-8");


doc.save(out,4,QDomNode::EncodingFromTextStream);


file.close();
}


void readXml::OnReadBtn()
{
  QFile file("test.xml");
 
  if (!file.open(QIODevice::ReadWrite /*| QIODevice::Truncate */|QIODevice::Text))
 
  return ;


QDomDocument doc;


QString vErrStr;
int vErrLine;
int vErrColumn;
if (!doc.setContent(&file, false, &vErrStr, &vErrLine, &vErrColumn))
{
// qDebug() << vErrStr;
file.close();
return;
}



QDomElement em = doc.documentElement();




QString str = em.tagName();


QDomElement ex = em.firstChildElement();


QString strex = ex.tagName();


ex = ex.firstChildElement();
QString strecxn = ex.attribute("key");
//下一个node
ex = ex.nextSiblingElement();
QString strexn = ex.tagName();
}
0 0