Unsolved Incorrect order of xml attributes-Qt 5.6.2
-
Hello All,
As I explored, because of QHash randomisation I am getting random ordered xml attributes.In order to fix it(to get deterministic order) I followed some suggestions by locking the hash seed value by the following code before my Qt application starts.
extern Q_CORE_EXPORT QBasicAtomicInt qt_qhash_seed;
qt_qhash_seed.store(0);
But still I am getting random ordered xml attributes only.Does anyone have clue on this?
Thanks in advance. -
@Mallikarjun Why not just use QMap instead?
From the documentation:
"When iterating over a QMap, the items are always sorted by key" -
QHash is not supposed to keep an ordered list. it is equivalent in functionality to std::unordered_map. if you want order use QMap
-
@jsulm ,@VRonin I am not using QHash explicitly. I am using Qt's XML framework which internally uses QHash..
From the document :http://doc.qt.io/qt-5/qhash.html#qGlobalQHashSeed
"This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function."I tried setting the global hash seed using the code qSetGlobalQHashSeed(42); but still my xml attributes are not in order.
-
Hi,
Please show the code where you call the XML classes. Maybe you can use another class instead that does not show the problem, e.g. XML Stream Iterator.
-Michael. -
@Mallikarjun said in Incorrect order of xml attributes-Qt 5.6.2:
temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization
You misread the documentation. this does not mean it will be ordered. This ensures that if you run the application 2 times the elements will still be shuffled but in the same way i.e. it will be shuffled always in the same way but still shuffled.
I stand by:
programs should never depend on a particular QHash ordering
Why do you need ordered attributes?!
-
@m-sue Below is my code :
int main(int argc, char *argv[])
{QCoreApplication a(argc, argv); *// Create a document to write XML* QDomDocument document; *// Making the root element* QDomElement root = document.createElement("address"); root.setAttribute("door",12345); root.setAttribute("name", QString()); root.setAttribute("pin", 54321); *// Adding the root element to the docuemnt* document.appendChild(root); *// Writing to a file* QFile file("C:/temp/myXLM.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Open the file for writing failed"; } else { QTextStream stream(&file); stream << document.toString(); file.close(); qDebug() << "Writing is done"; } return a.exec();
}
I am expecting the output like this :<address door="12345" name="" pin="54321"/>
but I am getting the output like this: <address name="" pin="54321" door="12345"/> and attributes order is keep on changing every time I build and run the application. -
@VRonin
Why do you need ordered attributes?!
-----> It is a problem when you track changes of output XML files using GIT, SVN, etc -
Below is my code :
if that is your real code I see no reason why you should use the xml module at all.
QXmlStreamReader
andQXmlStreamWriter
from QtCore are way better suited for the task.It is a problem when you track changes of output XML files
Use an XML tool like http://prettydiff.com/ and it will not show differences just because the order of the attributes changes