Qt的XML文件处理

上次随便写了些关于Qt5的事件与处理,没想到还会有人看,所以这次就认真写了一篇关于Qt中的XML文件处理的文章,如有知识上的错误还望大家指点~


这篇文章主要是解决以下问题:(如果需要的话可以参考一下)

·XML文件的存放

·XML文件的解析

·XML文件信息的查询



·XML文件的存放

XML文件的存放对知道的人来说简单,但是对没用过Qt的人来说放错地方了那可真是要命,直接会导致从一开始文件就读取错误

XML文件与头文件、源文件不同,而是放在对应的debug文件夹或release文件夹内


·XML文件的解析

文件的解析依然是那些老套路

    QFile *file;
    file = new QFile("attribs.xml");     //关联attrib.xml文件


void MainWindow::ReadFile() //读取文件

{
    if(!file->open(qiodevice::ReadOnly))    //以只读的方式打开文件
    {
        return;
    }
    if(!filedata.setContent(file))  //解析XML文件并设置为filedata的内容
    {                               //This function parses the XML document from the byte array data and sets it as the content of the document.
        file->close();
        return;
    }
    file->close();
}

setContent()返回解析结果,结果为bool值,解析成功后会返回true,否则返回false

·XML文件信息的查询

查询信息之前先要创建好XML文件,例:

<?xml version="1.0" encoding="UTF-8"?>
    <archive>
        <car id="1" >
            <attrib number="01" >排量:2393ml</attrib>
            <attrib number="02" >价格:43.26万元</attrib>
            <attrib number="03" >排放:欧 4</attrib>
            <attrib number="04" >油耗:7.01(90km/h) 8.31(120km/h)</attrib>
            <attrib number="05" >功率:130/6000</attrib>
        </car>
        <car id="2" >
            <attrib number="01" >排量:1600ml</attrib>
            <attrib number="02" >价格:8.26万元</attrib>
            <attrib number="03" >排放:欧 3</attrib>
            <attrib number="04" >油耗:6.11(90km/h)</attrib>
            <attrib number="05" >功率:68/5800</attrib>
        </car>
    </archive>

 
 
 ·搜寻节点
void MainWindow::showDetail()      //搜寻节点
{
    QDomNodeList cars = filedata.elementsByTagName("car");      //查找所有"car"元素节点,返回QDomNodeList实例

    //检测节点数用
    //int textNumber = cars.count();

    QString ID("2");

    for(int i = 0; i < cars.count(); i++)   //遍历节点
    {
        QDomNode car = cars.item(i);
        QString test(car.toElement().attribute("id"));      //toElement()函数将QDomNode转化为QDomElement,attribute("id")查找id属性,返回值为QString
        if(car.toElement().attribute("id") == ID)        //这里的ID是检验用的,来匹配元素节点中id对应为ID的节点(可调整)
        {
            getAttribList(car);     //得到节点后进一步查询信息
            break;
        }
    }
}

 ·获取信息
 
void MainWindow::getAttribList(QDomNode &car)   //获取信息
{
    QDomNodeList nodes = car.childNodes();  //childNodes()返回节点的子节点集合,返回值为QDomNodeList实例
    QDomNode node;
    QString getNumber;
    for(int i = 0; i < nodes.count(); i++)  //以QDomNodeList的nodes的子节点数来遍历子节点提取需要的信息
    {
        node = nodes.item(i);
        getNumber = node.toElement().attribute("number");   //获取子节点的number属性对应信息
        QString infor(getNumber+" "+node.toElement().text());   //node.toElement().text()对应节点元素文本

        QMessageBox::about(this,"infor",infor);
    }
}
 
 
 
//下面是源码(xml文件用上面的就可以了):
 
 
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFile>
#include <QDomDocument>
#include <QDomNode>
#include <QMessageBox>
#include <QDomNodeList>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);

    QFile *file;

    QDomDocument filedata;

    void showDetail();
    void ReadFile();
    void getAttribList(QDomNode &car);

    ~MainWindow();
};

#endif // MAINWINDOW_H
 
 
 
 
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    file = new QFile("attribs.xml");     //关联attrib.xml文件

    ReadFile();

    showDetail();

}

void MainWindow::showDetail()      //搜寻节点
{
    QDomNodeList cars = filedata.elementsByTagName("car");      //查找所有"car"元素节点,返回QDomNodeList实例

    //检测节点数用
    //int textNumber = cars.count();

    QString ID("2");

    for(int i = 0; i < cars.count(); i++)   //遍历节点
    {
        QDomNode car = cars.item(i);
        QString test(car.toElement().attribute("id"));      //toElement()函数将QDomNode转化为QDomElement,attribute("id")查找id属性,返回值为QString
        if(car.toElement().attribute("id") == ID)        //这里的ID是检验用的,来匹配元素节点中id对应为ID的节点(可调整)
        {
            getAttribList(car);     //得到节点后进一步查询信息
            break;
        }
    }
}

void MainWindow::getAttribList(QDomNode &car)   //获取信息
{
    QDomNodeList nodes = car.childNodes();  //childNodes()返回节点的子节点集合,返回值为QDomNodeList实例
    QDomNode node;
    QString getNumber;
    for(int i = 0; i < nodes.count(); i++)  //以QDomNodeList的nodes的子节点数来遍历子节点提取需要的信息
    {
        node = nodes.item(i);
        getNumber = node.toElement().attribute("number");   //获取子节点的number属性对应信息
        QString infor(getNumber+" "+node.toElement().text());   //node.toElement().text()对应节点元素文本

        QMessageBox::about(this,infor);
    }
}

void MainWindow::ReadFile()
{
    if(!file->open(qiodevice::ReadOnly))    //以只读的方式打开文件
    {
        return;
    }
    if(!filedata.setContent(file))  //解析XML文件并设置为filedata的内容
    {                               //This function parses the XML document from the byte array data and sets it as the content of the document.
        file->close();
        return;
    }
    file->close();
}

MainWindow::~MainWindow()
{

}

main.cpp文件未做修改

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念