Wrong attribute order in Qt XML

I have the following code:

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

I thought we get the XML as:

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

But I have:

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

Why?

PS Yes, of course, this does not matter in the XML document, but I want to get the correct order of the attributes.

+4
source share
3 answers

I understand this is a partial answer to the old question, but if you just want the attribute order to be consistent every time there is a way to achieve this in Qt5.

, -. QHash , DDOS-. :

QHash . QHash, , , . . , QT_HASH_SEED. , , QHash().

, , , QT_HASH_SEED, . , Qt Creator "":

Where to set QT_HASH_SEED in the Run settings in the Project tab in Qt Creator

, .

+6

xml git. ( ) xml, . , Qt Xml QXmlStreamWriter.

QT_HASH_SEED ( @MrEricSir) . , :

qSetGlobalQHashSeed(42); // 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);

, -, .

+2

There is no such thing as a “proper” order of XML attributes. Standard XML implementations cannot take care of this, and they do not, and rightly so.

Human readability is the only reason to worry about attribute ordering. If you want to write human-readable XML, you may need to roll up your own code.

0
source

Source: https://habr.com/ru/post/1525102/


All Articles