Qt:利用DOM读写XML小测

来源:互联网 发布:服装批发开单软件 编辑:程序博客网 时间:2024/06/16 22:21
Qt:利用DOM读写XML小测

//DOM写/修改XML:

#include <iostream>#include <QtXml>using namespace std;int main(int argc, char *argv[]){    QFile file("domwrite.develop");if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){cout << "Can't create file!" << endl;return 1;}QDomDocument doc;QDomText text;QDomElement element;QDomProcessingInstruction instruction;instruction = doc.createProcessingInstruction("xml","version=\'1.0\'");doc.appendChild(instruction);    QDomElement root = doc.createElement("develop");doc.appendChild(root);QDomElement general = doc.createElement("general");root.appendChild(general);element = doc.createElement("author");    text = doc.createTextNode("kai");element.appendChild(text);general.appendChild(element);element = doc.createElement("email");    text = doc.createTextNode("kai@163.com");element.appendChild(text);general.appendChild(element);element = doc.createElement("version");text = doc.createTextNode("$VERSION");element.appendChild(text);general.appendChild(element);element = doc.createElement("projectmanagement");text = doc.createTextNode("KdevTrollProject");element.appendChild(text);general.appendChild(element);element = doc.createElement("primarylanguage");text = doc.createTextNode("C++");element.appendChild(text);general.appendChild(element);QDomElement keywords = doc.createElement("keywords");element = doc.createElement("keyword");text = doc.createTextNode("C++");element.appendChild(text);keywords.appendChild(element);general.appendChild(keywords);element = doc.createElement("projectname");text = doc.createTextNode("domwrite");element.appendChild(text);general.appendChild(element);element = doc.createElement("ignoreparts");general.appendChild(element);element = doc.createElement("projectdirectory");text = doc.createTextNode(".");element.appendChild(text);general.appendChild(element);element = doc.createElement("absoluteprojectpath");text = doc.createTextNode("false");element.appendChild(text);general.appendChild(element);element = doc.createElement("description");general.appendChild(element);element = doc.createElement("defaultencoding");general.appendChild(element);QDomElement kdevfileview = doc.createElement("kdevfileview");QDomElement groups = doc.createElement("groups");element = doc.createElement("group");QDomAttr pattern = doc.createAttribute("pattern");pattern.setValue("*.cpp;*.cxx;*.h");QDomAttr name = doc.createAttribute("name");name.setValue("Sources");element.setAttributeNode(pattern);element.setAttributeNode(name);groups.appendChild(element);kdevfileview.appendChild(groups);root.appendChild(kdevfileview);QTextStream out(&file);doc.save(out, 4);return 0;}
//DOM读XML文件:

void DrawWidget::readSvg(QString fileName){QFile file(fileName);if (!file.open(QIODevice::ReadOnly))return;    if (!doc.setContent(&file)) { //QDomDocument doc;file.close();return;}file.close();bDraw = true;update();}void DrawWidget::paintEvent(QPaintEvent *event){if(!bDraw)return;QPainter painter(this);    QDomElement root = doc.documentElement();  //返回文档的根元素    QDomElement child = root.firstChildElement();QRect rect;    while (!child.isNull()) {        if(child.tagName() == "rect") {setPenAndBrush(painter, child);rect.setRect(child.attribute("x").toInt(),child.attribute("y").toInt(),child.attribute("width").toInt(),child.attribute("height").toInt());painter.drawRect(rect);}else if (child.tagName() == "circle") {setPenAndBrush(painter, child);int cx=child.attribute("cx").toInt();int cy=child.attribute("cy").toInt();int r=child.attribute("r").toInt();rect.setRect(cx-r, cy-r, 2*r, 2*r);painter.drawEllipse(rect);}else if (child.tagName() == "text") {setPenAndBrush(painter, child);int x=child.attribute("x").toInt();int y=child.attribute("y").toInt();QFont font;font.setFamily(child.attribute("font-family"));font.setPointSize(child.attribute("font-size").toInt());painter.setFont(font);painter.drawText(x, y, child.text());}else if (child.tagName() == "polyline") {setPenAndBrush(painter, child);QString points = child.attribute("points");QStringList list = points.split(" ");QPoint pointArray[100];int i=0;foreach(QString point, list) {int index = point.indexOf(",");pointArray[i].setX(point.left(index).toInt());pointArray[i].setY(point.mid(index + 1).toInt());i++;}painter.drawPolyline(pointArray, i);}        child = child.nextSiblingElement();    }}void DrawWidget::setPenAndBrush(QPainter& painter, QDomElement& domElement){bool ok;QPen pen;QBrush brush;QString fill = domElement.attribute("fill");if(fill != "none") {QRgb color = (QRgb)fill.toInt(&ok, 16);  brush.setColor(QColor(color));}    QString stroke = domElement.attribute("stroke"); //画笔的一划if(stroke != "none") {if(stroke == "blue")pen.setColor(Qt::blue);else if(stroke == "red")pen.setColor(Qt::red);else {QRgb color = (QRgb)stroke.toInt(&ok, 16);pen.setColor(QColor(color));}}QString strokeWidth = domElement.attribute("stroke-width");pen.setWidth(strokeWidth.toInt());painter.setPen(pen);painter.setBrush(brush);}


为了在Qt程序中使用XML(可扩展标记语言) API,必须在qmake工程文件中加入:QT += xml

包含头文件:#include <QtXml>
QtXml模块提供了三种不同的应用程序编程接口来读取XML文档:
1、QXmlStreamReader是一个用于读取格式良好的XML文档的快速解析器。最快最易于使用,适合编写单通解析器。
2、DOM(Document Object Modal)方法将XML文件表示成一棵树,便于随机访问其中的节点,但消耗内存相对多一些;
3、SAX(Simple)是一种时间驱动的XML API,速度快,但不便于随机访问任意节点。
对于XML文件的写入,Qt也提供了三种可用的方法:
1、使用QXmlStreamWriter:最简单易行。
2、在内存中以DOM树的结构表示数据,并要求这个树形结构将自己写到文件中(DOM树以作为应用程序的基本数据结构时才有意义)。
3、手动生成XML。


0 0
原创粉丝点击