Details
-
Bug
-
Resolution: Fixed
-
P3: Somewhat important
-
None
-
4.5.3
-
None
Description
t4id: 246955
When using QDomDocument::toString() to generate XML files, some attributes order are incorrect. As a result, when parsing the generated XML file, the namespaceURI is not properly set.
Qt is generating xml that looks like this:
<element test:my-attribute="true" xmlns:test="my:test:uri" />,
Since the xmlns:test declaration is coming after the usage of the test namespace, the my-attribute QAttrNode isn't getting its namespaceURI set.
Reversing the things: <element xmlns:test="my:test:uri" test:my-attribute="true" /> and this parses fine.
The following test program reproduces the problem:
#include <QDebug> #include <QDomDocument> int main( int argc, char **argv ) { QDomDocument testDom( "test" ); QDomElement testElement = testDom.createElement( "element" ); testElement.setAttributeNS( "my:test:uri", "test:attribute", "true" ); testDom.appendChild( testElement ); qDebug() << testDom.toString(); QByteArray domBytes( testDom.toByteArray() ); QDomDocument newDom( "test" ); newDom.setContent( domBytes, true ); // turn namespacing on qDebug() << newDom.toString(); QDomElement newElement = newDom.firstChild().toElement(); qDebug() << newElement.attributeNS( "my:test:uri", "attribute" ); // should print "true" return 0; }
The customer also provided a candidate patch:
--- dom/qdom.cpp (revision 72793) +++ dom/qdom.cpp (working copy) @@ -4161,7 +4161,6 @@ if (namespaceURI.isNull()) { s << name << "=\"" << encodeText(value, s, true, true) << '\"'; } else { - s << prefix << ':' << name << "=\"" << encodeText(value, s, true, true) << '\"'; /* This is a fix for 138243, as good as it gets. * * QDomElementPrivate::save() output a namespace declaration if @@ -4177,6 +4176,9 @@ ownerNode->prefix != prefix) { s << " xmlns:" << prefix << "=\"" << encodeText(namespaceURI, s, true, true) << '\"'; } + + s << ' '; + s << prefix << ':' << name << "=\"" << encodeText(value, s, true, true) << '\"'; } } @@ -4540,7 +4542,6 @@ if (it.value()->namespaceURI.isNull()) { s << it.value()->name << "=\"" << encodeText(it.value()->value, s, true, true) << '\"'; } else { - s << it.value()->prefix << ':' << it.value()->name << "=\"" << encodeText(it.value()->value, s, true, true) << '\"'; /* This is a fix for 138243, as good as it gets. * * QDomElementPrivate::save() output a namespace declaration if @@ -4558,6 +4559,9 @@ s << " xmlns:" << it.value()->prefix << "=\"" << encodeText(it.value()->namespaceURI, s, true, true) << '\"'; outputtedPrefixes.insert(it.value()->prefix); } + + s << ' '; + s << it.value()->prefix << ':' << it.value()->name << "=\"" << encodeText(it.value()->value, s, true, true) << '\"'; } s << ' '; }
Attachments
Issue Links
- depends on
-
QTBUG-8158 QDomDocument save reorders attributes
- Closed
- relates to
-
QTBUG-76800 Option to sort attributes in QDomDocument::toString()
- Reported