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

}
相关推荐
郭小李6 小时前
mybatis的 Mapper.xml语法
xml·java·mybatis
ShineWinsu6 小时前
对于Qt:QLabel常见属性的解析
c++·qt·面试·笔试·gui·标签·qlabel
步行cgn6 小时前
为什么既要 web.xml 又要 springmvc.xml?能不能只配一个?
xml·前端·hive
小徐不徐说6 小时前
数据库存储基础:理解表结构、主键、事务与 BLOB
开发语言·数据库·c++·qt·程序设计
蓝祁子yoky1 天前
Qt学习日记 Day13:网络编程(一)-TCP基础与Socket实战
网络·qt·学习
飞浪纪元[FWC–FE]1 天前
从零到一键绿:Qt 5.14.2 + VS2019 开发环境搭建、Kit 套件配置与构建错误修复完全手册
开发语言·qt
ShineWinsu1 天前
对于Qt:QCheckBox常见属性的详细解析
c++·qt·面试·笔试·gui·qcheckbox·多选框
ShineWinsu1 天前
对于Qt:QPushButton常见属性的解析
开发语言·c++·qt·api·gui·按钮·qpushbutton
飞浪纪元[FWC–FE]1 天前
Qt 工程创建与 CMake / qmake 构建规范
开发语言·数据库·qt
Lhan.zzZ1 天前
用 Qt 构建线程安全的通讯静态库:从线程亲和性到完整工程
c++·vscode·qt·tcp/ip·udp