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

}
相关推荐
bkspiderx5 小时前
在Visual Studio中安装与配置Qt插件(Qt Visual Studio Tools)
ide·qt·visual studio·qt插件
步峰代理-盾山7 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
ShineWinsu2 天前
对于Qt:信号量(QSemaphore)的解析
c++·qt·面试·笔试·客户端·线程安全·信号量
ShineWinsu3 天前
对于Qt:条件变量(QWaitCondition)的解析
c++·qt·面试·线程·条件变量·客户端·锁
郝学胜-神的一滴3 天前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
敲上瘾3 天前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
ShineWinsu4 天前
对于Qt:事件分发器以及事件过滤器的解析
c++·qt·面试·gui·客户端·事件过滤器·事件分发器
ShineWinsu5 天前
对于Qt:事件的解析
c++·qt·面试·笔试·gui·客户端·事件
西西弗Sisyphus5 天前
Qt QML 程序是怎么跑起来的(1)qml-only
开发语言·c++·qt
小灰灰搞电子5 天前
QAccel 类讲解与代码示例:Qt 3 中的键盘加速键管理
qt·计算机外设