I am using the QXmlStreamWriter class to serialize a bunch of settings to an XML string. I'm using version 4.4.3-0ubuntu1.1 which is the latest at the moment of posting.

The problem I'm having is that when I write an attribute using
Qt Code:
  1. writeAttribute( "DEFAULTnamespaceUri", "attributeName", "attributeValue" );
To copy to clipboard, switch view to plain text mode 
it doesn't recognize that the namespaceUri is already defined (as default), so it automatically declares a new namespace for it. A code example will probably make it clearer:

Qt Code:
  1. void MainWindowImpl::totallyAwesomeSerialization()
  2. {
  3. QString xml;
  4. QXmlStreamWriter xmlWriter( &xml );
  5. xmlWriter.setAutoFormatting( true );
  7. xmlWriter.writeDefaultNamespace( "urn:mpeg:mpeg21:2003:01-DIA-NS" );
  8. xmlWriter.writeNamespace( "urn:mpeg:mpeg7:schema:2001", "mpeg7" );
  10. // write the <?xml... declaration
  11. xmlWriter.writeStartDocument();
  13. // write the root element
  14. xmlWriter.writeStartElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "DIA" );
  16. // write another random element, in the default namespace
  17. xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "FirstChildElement" );
  19. // add an attribute to the element, again in the default namespace
  20. xmlWriter.writeAttribute( "urn:mpeg:mpeg21:2003:01-DIA-NS", "bloink", "oink" );
  22. // write yet another element
  23. xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "SecondChildElement" );
  25. // write another attribute, this time NOT in the default namespace
  26. xmlWriter.writeAttribute( "urn:mpeg:mpeg7:schema:2001", "foo", "bar" );
  28. // close all tags
  29. xmlWriter.writeEndDocument();
  31. xmlEditor->setPlainText( xml );
  32. }
To copy to clipboard, switch view to plain text mode 

This piece of code has the following result:

Qt Code:
  1. <?xml version="1.0"?>
  2. <DIA xmlns="urn:mpeg:mpeg21:2003:01-DIA-NS" xmlns:mpeg7="urn:mpeg:mpeg7:schema:2001">
  3. <FirstChildElement xmlns:n1="urn:mpeg:mpeg21:2003:01-DIA-NS" n1:bloink="oink"/>
  4. <SecondChildElement mpeg7:foo="bar"/>
  5. </DIA>
To copy to clipboard, switch view to plain text mode 

As you can see, the QXmlStreamWriter knows the namespace of "SecondElement" is the default one, so it doesn't need to declare anything or prefix it. When writing the attribute, it looks like it doesn't remember the default namespace, so it automatically makes a new one for it ("n1" in the example above). The problem doesn't seem to exist with non-default namespaces where it finds the correct namespace prefix ("mpeg7" in the example above).

I will probably use the writeAttribute( qualifiedName, value ) as a short term solution.

I wonder what I'm doing wrong? And how do I solve it?