Important: Please read the Qt Code of Conduct -

Question about XML header

  • Hi,
    I have seen this instruction used in several projects:


    Specifically a line like this:

    doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");

    My question is: To include this header within an XML file add some special feature for real? If I do something like:

    QDomDocument document;

    That header has any impact on the setContent() method behavior?

    I appreciate any hint. Thanks!

  • Moderators

    Those are two different use cases.

    The first snippet is usually used when you create the xml, i.e. build a document and store it. In this case you need it or the consumer of such xml will not know how to handle the file.

    The other one is when you open up an existing document, e.g. from file. It will most probably already have the version and encoding declaration in that file already. When you setContent() you effectively replace the dom tree so anything that was there before, be it a processing instruction, nodes or attributes, is gone anyway.

    So to answer your question:
    You should add it when you hand craft and store a new xml.
    When you read a file and call setContent()it does nothing as it is basically discarded with the rest of pre-existing content.

    Btw. technically version and encoding are not even processing instructions but since the syntax is the same it just works.

Log in to reply