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);

}
相关推荐
小c君tt1 小时前
QT中想在QTextEdit控件中使用Qslog日志输出出现问题原因及解决方法
开发语言·qt
SunkingYang3 小时前
QT程序怎么接收MFC通过sendmessage发送的信号
qt·mfc·信号·事件·sendmessage·接收消息
SunkingYang4 小时前
Qt中QString 查找子串的完整指南
qt·字符串·qstring·子字符串·查找子串
世转神风-5 小时前
qt-在字符串中指定位置插入字符串
开发语言·qt
hqwest6 小时前
码上通QT实战03--登录逻辑
开发语言·qt·登录·嵌入式实时数据库·界面设计
世转神风-8 小时前
QEventLoop与QTimer联动
qt
SunkingYang9 小时前
QT如何将char*转QString
qt·qstring·指针·转换·char
hqwest11 小时前
码上通QT实战01--创建项目
开发语言·qt·sqlite3·qt项目·qwidget·qwindow
深蓝海拓12 小时前
PySide6从0开始学习的笔记(十二) QProgressBar(进度条)
笔记·python·qt·学习·pyqt