XML(可扩展标记语言)

复制代码
QDomDocument doc;
   QDomElement  ss= doc.createElement("root");//创建标签
//ss标签添加到文档对象
    doc.appendChild(ss);
    //doc.save()
    auto hero = doc.createElement("hero");
    ss.appendChild(hero);

    hero.setAttribute("id",10086);//为hero添加属性
    //属性名不能以数字开头

    //html中
    //<root>  <hero id = "10086"><abc>无极剑圣<abc></hero>  <abc></root>
//当添加孩子时就变成双标签了
    auto name = doc.createElement("abc");

    hero.appendChild(name);//hero的子标签
//html 中 <h1>增加内容<h1>
    hero.appendChild( doc.createTextNode("无极剑圣"));
   // doc.createTextNode("蛮族之王");

   QString filename  = QFileDialog::getSaveFileName(this,"保存xml");
    QFile ff(filename);
    if(!ff.open(QIODevice::WriteOnly))
    {
        QMessageBox::information(NULL,"信息提醒","打开失败");
        return;
    }

    //文本流
    QTextStream outstream(&ff);//获取文件的地址 创建文本流对象
    doc.save(outstream,4); //第二个参数缩进 4
    //默认utf-8 参数可不填

    ff.close();//记住文件得关闭

123.xml

读取测试

相关推荐
Hui Baby15 小时前
回顾-springboot自定义xml
xml·java·spring boot
开始了码1 天前
XML文件介绍和QT相关操作
xml·qt
Andy工程师2 天前
logback-spring.xml优先级更高
xml·spring·logback
小江村儿的文杰2 天前
UE4在MacOS上将Commit.gitdeps.xml设为Git LFS文件的潜在弊端
xml·ue4
humstone2 天前
基于xml 和sql 实现自定义报表查询
xml·数据库·sql
就叫飞六吧3 天前
考古spring.xml注册bean无法扫描目录问题
xml·java·spring
Star abuse3 天前
XML转YOLO格式数据集教程
xml·人工智能·yolo
gAlAxy...4 天前
MyBatis 核心配置文件 SqlMapConfig.xml 全解析
xml·mybatis
编程修仙5 天前
第十一篇 Spring事务
xml·java·数据库·spring
金士顿5 天前
Ethercat耦合器添加的IO导出xml 初始化IO参数
android·xml·java