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();
}
- qt DOM xml 读写
- Qt 读写XML文件
- QT 读写Xml文件
- QT读写XML
- Qt读写XML文件
- qt读写xml文件
- Qt 读写xml文件
- QT读写XMl
- QT 读写XML
- Qt 读写 XML
- QT XML文件的读写
- Qt中xml的读写
- QT使用流读写XML
- Qt读写xml文件实例
- QT QDomDocument 读写XML文件
- QT读写xml文件示例
- Qt中读写xml文件
- QT中XML的读写
- C++类四个默认函数---构造函数、析构函数、拷贝函数、赋值函数
- openwrt个性化固件编译
- Comet:基于 HTTP 长连接的“服务器推”技术
- ReactJS+Compass+Gulp+RESTful多页应用组件化开发
- 浏览器环境下JavaScript脚本加载与执行探析之代码执行顺序
- QT 读写XML
- mongoDB shardKey
- Mac下常用工具软件
- windbg Conditional breakpoints 条件断点
- Xamarin 编译错误: The file "obj-Release-android-bin-package_resources" does not exist
- 程序运行时间为0的问题
- org.eclipse.wst.common.project.facet.core.xml读不到
- 安卓文件操作
- Mysql相关笔记