xml去掉命名空间前缀n1

QXmlStreamWriter在读取111.xml时,会同时读取属性和命名空间

将属性和命名空间写入222.xml时,xmlns会增加前缀n1

111.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<SCL xmlns="https://www.baidu.com/" 
xmlns:xsi="https://www.baidu.com//2001/XMLSchema-instance" xsi:schemaLocation="https://www.baidu.com/ 123" xmlns:schemaLocation="https://www.baidu.com/456"/>

写入222.xml时

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<SCL xmlns:n1="https://www.baidu.com/" 
xmlns:xsi="https://www.baidu.com//2001/XMLSchema-instance" schemaLocation="https://www.baidu.com/ 123" xmlns:schemaLocation="https://www.baidu.com/456"/>

此时QXmlStreamWriter会自动增加命名空间前缀n1

**xmlns**是 XML 命名空间的标准前缀,用于声明命名空间。它告诉解析器某个前缀对应的是哪个 URI

writeDefaultNamespace ( const QString & namespaceUri );

writeDefaultNamespace("https://www.baidu.com/")

使用这个函数设置一下默认namespaceUri就可以了

对应命名空间前缀重复的schemaLocation

下一级的会被忽略

我们需要手动加上,在遍历写入属性时,判断一下改属性的namespaceUri

cpp 复制代码
 if (attrName == "schemaLocation" && basic_ssd_info_tmp.scl_attr.at(i).namespaceUri() == "https://www.baidu.com//2001/XMLSchema-instance") 
{
  // 特别处理 schemaLocation 属性,确保带有 xsi 前缀
  xmlWriter.writeAttribute("xsi:schemaLocation", attrValue);
} 
else {
 // 其他属性直接写入
 xmlWriter.writeAttribute(attrName, attrValue);

}
相关推荐
Felix_One3 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit6 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
范特西.i11 天前
QT聊天项目(8)
开发语言·qt
前网易架构师-高司机11 天前
带标注的驾驶员安全带识别数据集,识别率99.5%,可识别有无系安全带,支持yolo,coco json,pascal voc xml格式
xml·yolo·数据集·交通·安全带
枫叶丹411 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
逍遥德11 天前
Maven教程.01- settings.xml 文件<profile>使用详解
xml·java·maven
十五年专注C++开发11 天前
Qt deleteLater作用及源码分析
开发语言·c++·qt·qobject
kangzerun11 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
金刚狼8811 天前
qt和qt creator的下载安装
开发语言·qt
追烽少年x11 天前
Qt中使用Zint库显示二维码
qt