Qt XML中的属性顺序不正确 [英] Incorrect order of attributes in Qt XML

查看:203
本文介绍了Qt XML中的属性顺序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

element.clear();
element.setTagName("accountpoint");
element.setAttribute("code", QString(ID_CONST)+serial);
element.setAttribute("name", QString());
element.setAttribute("serial", serial);

QFile file(filename);
file.open(QIODevice::ReadWrite);
QTextStream stream(&file);
doc.save(stream, 4);

我以为我会得到像这样的XML:

I thought I will get an XML like:

<accountpoint code="4871583314750580" name="" serial="14750580">

但是我有

<accountpoint serial="14750580" code="4871583314750580" name="">

为什么?

P.S.是的,当然,在XML文档中没有关系,但是我想获得正确的属性顺序.

P.S. Yes, of course, in XML document it does not matter, but I want to get the right order of attributes.

推荐答案

尝试在git中存储xml设置数据时遇到了这个问题.在这种情况下(为了获得合理的差异),重要的是每次使用相同的属性排序来存储xml.该代码库已有数年历史,使用的是不推荐使用的 Qt Xml QXmlStreamWriter .

I ran into this issue when trying to store xml setting data in git. In this case (in order to get a sane diff) it is important to store the xml using the same attribute ordering each time. The code base was several years old, using the deprecated Qt Xml instead of the newer QXmlStreamWriter.

在这种情况下,设置QT_HASH_SEED环境变量的技巧(根据@MrEricSir的回答)效果很好.但是,也可以直接在代码中完成,例如以下示例:

The trick of setting the QT_HASH_SEED environment variable (from @MrEricSir's answer) works well in this case. However, it can also be done directly in code, like in this example:

qSetGlobalQHashSeed(0); // set a fixed hash value

QDomDocument doc = QDomDocument(); 
// add stuff to doc...
// ...

// save doc to file:
QFile file(filename);
QTextStream stream(&file);
stream << doc.toString(4);
file.close();

// reset hash seed with new random value.
qSetGlobalQHashSeed(-1);

这样,您的应用程序的其余部分将像以前一样工作,从而避免暴露于

This way, the rest of your application works as before, thus avoiding exposure to algorithmic complexity attacks.

这篇关于Qt XML中的属性顺序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆