Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-5364

QDomDocument::toString() generated file doesn't parse properly

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 4.5.3
    • Fix Version/s: None
    • Component/s: XML: DOM
    • Labels:
      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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                jasmcdon Jason McDonald (Closed Nokia Identity. Please assign to "macadder" instead) (Inactive)
              • Votes:
                3 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes