• Qt中利用QDomDocument读写xml小Demo


    1.写入xml文档中

    bool protProjectInfo::SaveXml() 
    {
    	//QFile file(mProjectInfoPath.filePath("projectconfig.xml"));    
         QFile file("c:/projectconfig.xml");
    	if(! file.open(QIODevice::WriteOnly)) {
    		//logger()->error("open file fail '%1'.",mProjectInfoPath.filePath("projectconfig.xml"));
    		file.close();
    		return false;
    	}
    	QDomDocument projectinfodom;
    	projectinfodom.appendChild( projectinfodom.createProcessingInstruction("xml","version="1.0" encoding="UTF-8""));
    	QDomElement projectelement = projectinfodom.createElement("Project");
    	projectinfodom.appendChild(projectelement);
    	
    	QDomElement projectnameelement = projectinfodom.createElement("ProjectName");
    	projectnameelement.appendChild(projectinfodom.createTextNode(mProjectName));
    	projectelement.appendChild(projectnameelement);
    
    	QDomElement createtimeelement = projectinfodom.createElement("CreateTime");
    	createtimeelement.appendChild(projectinfodom.createTextNode(mCreateTime.toString("yyyy/MM/dd hh:mm:ss")));
    	projectelement.appendChild(createtimeelement);
    
    	QTextStream stream(&file);
    	projectinfodom.save(stream,4);
    	file.close();
    	return true;
    }
    

     2.读xml文件

    bool protProjectInfo::LoadXml()
    {
    	//QFile file(mProjectInfoPath.filePath("projectconfig.xml"));     
        QFile file("c:/projectconfig.xml"); 
    	if(! file.open(QIODevice::ReadOnly)) {
    		//logger()->error("Open '%1' failed,'%2'.",mProjectInfoPath.filePath("projectconfig.xml"),file.errorString());	
    		return false;
    	}
    	QDomDocument doc;
    	if (! doc.setContent(&file,true)) {
    		//logger()->error("QDomDocument setContent fail.");
    		file.close();
    		return false;
    	}
    	file.close();
    	QDomElement docrootelement = doc.documentElement();
    	QDomElement projectnameelement = docrootelement.firstChildElement("ProjectName");
    	QDomElement createtimeelement = docrootelement.firstChildElement("CreateTime");
    	
    	mProjectName = projectnameelement.text();
    	mCreateTime =  QDateTime::fromString(createtimeelement.text(),"yyyy/MM/dd hh:mm:ss");
    	return true;
    }
    

     3.注意事项

        
        1.protProjectInfo 是个简单类
    	    QDir mProjectInfoPath;
    
             QString mProjectName;
    
             QDateTime mCreateTime;
        2.xml格式如下
        <?xml version="1.0" encoding="UTF-8"?>
            <Project>
                <ProjectName>project09</ProjectName>
                <CreateTime>2015/07/22 16:22:05</CreateTime>
            </Project>
    
     QFile file("c:/projectconfig.xml");
  • 相关阅读:
    360°视频论文调研
    360°视频相机调研
    Oculus VR眼镜调研
    VR论文调研
    3ds Max学习日记(十)——显示场景资源管理器
    最近在学习Flask框架,那么就说下jinja2吧~~~
    最近学习了Sqlite3数据库,写一下操作应用以及命令
    HTML5游戏开发,剪刀石头布小游戏案例
    jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发
    jquery简单的大背景banner图片全屏切换
  • 原文地址:https://www.cnblogs.com/L-Lune/p/4670659.html
Copyright © 2020-2023  润新知