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

}
相关推荐
韭菜钟1 天前
在Qt中使用QuickJS
开发语言·qt
枫叶丹41 天前
【Qt开发】Qt窗口(三) -> QStatusBar状态栏
c语言·开发语言·数据库·c++·qt·microsoft
u***1371 天前
Tomcat的server.xml配置详解
xml·java·tomcat
t***31651 天前
QT开发:事件循环与处理机制的概念和流程概括性总结
开发语言·qt
四维碎片1 天前
【Qt】OpenGL渲染框架
qt·opengl
西游音月2 天前
(10)功能实现:Qt实战项目之新建项目对话框
开发语言·qt
宠..2 天前
使用纯代码设计界面
开发语言·c++·qt
i***71952 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
友友马2 天前
『QT』窗口 (二) - 深入剖析 QDialog 对话框机制与内存管理
开发语言·qt
刃神太酷啦2 天前
C++的IO流和C++的类型转换----《Hello C++ Wrold!》(29)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode