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
writeAttribute( "DEFAULTnamespaceUri", "attributeName", "attributeValue" );
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:
void MainWindowImpl::totallyAwesomeSerialization()
{
QXmlStreamWriter xmlWriter( &xml );
xmlWriter.setAutoFormatting( true );
xmlWriter.writeDefaultNamespace( "urn:mpeg:mpeg21:2003:01-DIA-NS" );
xmlWriter.writeNamespace( "urn:mpeg:mpeg7:schema:2001", "mpeg7" );
// write the <?xml... declaration
xmlWriter.writeStartDocument();
// write the root element
xmlWriter.writeStartElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "DIA" );
// write another random element, in the default namespace
xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "FirstChildElement" );
// add an attribute to the element, again in the default namespace
xmlWriter.writeAttribute( "urn:mpeg:mpeg21:2003:01-DIA-NS", "bloink", "oink" );
// write yet another element
xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "SecondChildElement" );
// write another attribute, this time NOT in the default namespace
xmlWriter.writeAttribute( "urn:mpeg:mpeg7:schema:2001", "foo", "bar" );
// close all tags
xmlWriter.writeEndDocument();
xmlEditor->setPlainText( xml );
}
void MainWindowImpl::totallyAwesomeSerialization()
{
QString xml;
QXmlStreamWriter xmlWriter( &xml );
xmlWriter.setAutoFormatting( true );
xmlWriter.writeDefaultNamespace( "urn:mpeg:mpeg21:2003:01-DIA-NS" );
xmlWriter.writeNamespace( "urn:mpeg:mpeg7:schema:2001", "mpeg7" );
// write the <?xml... declaration
xmlWriter.writeStartDocument();
// write the root element
xmlWriter.writeStartElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "DIA" );
// write another random element, in the default namespace
xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "FirstChildElement" );
// add an attribute to the element, again in the default namespace
xmlWriter.writeAttribute( "urn:mpeg:mpeg21:2003:01-DIA-NS", "bloink", "oink" );
// write yet another element
xmlWriter.writeEmptyElement( "urn:mpeg:mpeg21:2003:01-DIA-NS", "SecondChildElement" );
// write another attribute, this time NOT in the default namespace
xmlWriter.writeAttribute( "urn:mpeg:mpeg7:schema:2001", "foo", "bar" );
// close all tags
xmlWriter.writeEndDocument();
xmlEditor->setPlainText( xml );
}
To copy to clipboard, switch view to plain text mode
This piece of code has the following result:
<?xml version="1.0"?>
<DIA xmlns="urn:mpeg:mpeg21:2003:01-DIA-NS" xmlns:mpeg7="urn:mpeg:mpeg7:schema:2001">
<FirstChildElement xmlns:n1="urn:mpeg:mpeg21:2003:01-DIA-NS" n1:bloink="oink"/>
<SecondChildElement mpeg7:foo="bar"/>
</DIA>
<?xml version="1.0"?>
<DIA xmlns="urn:mpeg:mpeg21:2003:01-DIA-NS" xmlns:mpeg7="urn:mpeg:mpeg7:schema:2001">
<FirstChildElement xmlns:n1="urn:mpeg:mpeg21:2003:01-DIA-NS" n1:bloink="oink"/>
<SecondChildElement mpeg7:foo="bar"/>
</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?
Bookmarks