Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Issue with QDomDocument::setContent and XML declaration attribute quotes



  • Hello
    I'm having an issue when importing an XML document from a string.

    The XML declaration/preprocessor has two attributes (version, encoding) which are defined within double-quotes.
    However, on creating a QDomDocument via the setContent method, the double-quotes are replaced with single-quotes but only for the declaration/preprocessor element, the root element attributes still use double-quotes.

    QDomDocument::toString() :

    packet : 
    "<?xml version="1.0" encoding="UTF-8"?>
    <pkt uniqueID="fe87d654-dded-44e6-8c6a-70c92ebd4d7d" version="1">
     <inner uniqueID="dca0ddd7-2e91-492f-b61c-d4962a003dd9">
      <element>value</element>
     </inner>
    </pkt>"
    

    QDomDocument::SetContent(...) ... toString() :

    getPacket : 
    "<?xml version='1.0' encoding='UTF-8'?>
    <pkt uniqueID="fe87d654-dded-44e6-8c6a-70c92ebd4d7d" version="1">
     <inner uniqueID="dca0ddd7-2e91-492f-b61c-d4962a003dd9">
      <element>value</element>
     </inner>
    </pkt>"
    

    The data is being used in a transmission protocol where on the receiving end it needs to be verified via a checksum - this verification will fail due to the conversion from double-quotes to single-quotes.

    Any ideas on how I can preserve the use of double-quotes across conversion?

    Thanks

    See code below used to recreate the issue:

    int main( void )
    {
        QDomDocument packet = pDoc.document();
        QString stringPacket = packet.toString();
        qDebug() << "packet :" << endl << stringPacket;
    
        QString errorMessage; int errorLine; int errorColumn;
        QDomDocument getPacket;
        if( !(getPacket.setContent( stringPacket, &errorMessage, &errorLine, &errorColumn)) )
        {
            qDebug() << "!! error !!" << endl
                     << "message :" << errorMessage << endl
                     << "errorLine :" << errorLine << endl
                     << "errorColumn :" << errorColumn;
        }
        qDebug() << "getPacket :" << endl << getPacket.toString();
    }
    

  • Lifetime Qt Champion

    Hi,

    I think that the most quickly implemented solution would be to use QString's replace function and update the first line appropriately.

    Hope it helps.


Log in to reply