Qt Xml QDomDocument Special character handling



  • Hi,

    I have QString which contains XML data that is obtained from a remote system.
    The problem with this data is that the special characters are not translated. for example & is not translated to &amp.
    When I try to parse this xml using QDomDocument's setContent method, I experience a failure.

    Does Qt Xml handling any how have the facility of escaping such characters or replacing them with the translated characters so that the Xml document becomes a valid document?

    Any help is greatly appreciated.

    Please note that I cannot change the data format that I am receiving as the remote system is not under my control, so CDATA option is not available.

    Thanks


  • Moderators

    [quote author="prady_80" date="1383747289"]
    I have QString which contains XML data that is obtained from a remote system.
    The problem with this data is that the special characters are not translated. for example & is not translated to &amp.[/quote]
    As you may have noticed translating "&" to "&amp" also contains a "&" ;)

    Thus it's the responsibility of the remote system that all special characters are converted to their entities inside text-nodes.

    QString doesn't do any implicit entity conversations.

    What errors do you get when using QDomDocument::setContent()?



  • Hi,

    What is the text encoding used by your XML data?



  • Encoding is UTF-8 and "unexpected character" is the error I get.
    The & character is the first one to get translated
    Order of translation should therefore be
    '&' -> "&"
    first and
    anyone after that
    ''' -> "'"
    '"' ->"""
    '>' -> ">"
    '<' -> "<";



  • I have finally a solution that I don't like much.
    Posting it for any further comments.
    I use QXmlStreamReader to read the document and allow it to fail.
    I get the point of failure using characterOffset() . Using this offset peak into the buffer... if the error is correctable... correct it else report a failure.
    If corrected then re-parse to check validity ...do this till everything is correct or a true error happens.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.