Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Removing !DOCTYPE from the XML header
QtWS25 Last Chance

Removing !DOCTYPE from the XML header

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 13.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I am generatiing a xml file using QDomDocument.
    @QDomDocument mainDoc("?xml version=1.0?");@

    The above line of code generates the xml title as below
    <!DOCTYPE ?xml version=1.0?>

    I want to generate header as below
    <?xml version=1.0?>
    How to do that?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      You're doing it wrong™

      bq. "QDomDocument::QDomDocument ( const QString & name )":http://doc.qt.nokia.com/4.7/qdomdocument.html#QDomDocument-2
      Creates a document and sets the name of the document type to name.

      So, you're setting the root tag name to the string "?xml version=1.0?".

      Most probably this is what you want:

      @
      QDomDocument dom;
      QDomElement root = dom.createElement("MyRoot");
      dom.appendChild(root);
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Indrajeet
        wrote on last edited by
        #3

        HI volker

        This is not what i want to do.
        I want to set document name to <?xml version=1.0?>

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          What do you mean with "document name"?

          A valid XML file always starts with <? xml version="1.0" ?>. There is no way to change that without producing an invalid XML file. The root element - which can be seen as the "document name" - can be chosen freely but may only consist of a "limited set":http://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameStartChar of characters; it must not contain ?.

          1 Reply Last reply
          0
          • AlicemirrorA Offline
            AlicemirrorA Offline
            Alicemirror
            wrote on last edited by
            #5

            @Lukas, I think that Rajiveer is aware that a valid document is

            bq. A valid XML file always starts with <? xml version=“1.0” ?>

            It seems that his problem is that for some reasons he should manage a non-valid xml file with the !DOCTYPE included in the header to transform it in a valid XML document.
            If the data are in someway already in a stream it is possible to convert to QStream and apply some transform i.e. split the bad heather and remove the wrong characters, or use a string substitution api or something like this before saving the line somewhere.

            The strange question is that QDomDocument creates a wrong header ...

            Enrico Miglino (aka Alicemirror)
            Balearic Dynamics
            Islas Baleares, Ibiza (Spain)
            www.balearicdynamics.com

            1 Reply Last reply
            0
            • AlicemirrorA Offline
              AlicemirrorA Offline
              Alicemirror
              wrote on last edited by
              #6

              In the "QDomDocument class documentation":http://developer.qt.nokia.com/doc/qt-4.7/qdomdocument.html page you can read:

              To create a document using DOM use code like this:
              @
              QDomDocument doc("MyML");
              QDomElement root = doc.createElement("MyML");
              doc.appendChild(root);

              QDomElement tag = doc.createElement("Greeting");
              root.appendChild(tag);

              QDomText t = doc.createTextNode("Hello World");
              tag.appendChild(t);

              QString xml = doc.toString();
              @
              It seems that you stop to only the first step, so the valid XML document is not yet created until you append the nodes to - as I see from your example - Probably what you see is the pure string built by the class still incomplete. Try to add a fake node and close the sequence as in the example above (from the QDomDocument class documentation) to see if it works.
              Reading the documentation of this class it seems too that if you create the XML and add something wrong or do not add those stuff that it is expected the object is deleted.

              Enrico Miglino (aka Alicemirror)
              Balearic Dynamics
              Islas Baleares, Ibiza (Spain)
              www.balearicdynamics.com

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Well.

                Be aware that if you pass a QString to the QDomDocument constructor you will set the "document type":http://www.w3.org/TR/xml/#dt-doctype, not the name of the root element nor any processing instruction (<? ... ?>).
                @
                QDomDocument domDocument;

                QDomProcessingInstruction xmlProcessingInstruction = domDocument.createProcessingInstruction("xml", "version=\"1.0\"");
                domDocument.appendChild(xmlProcessingInstruction);
                
                QDomElement rootElement = domDocument.createElement("root");
                domDocument.appendChild(rootElement);
                
                QDomElement childElement = domDocument.createElement("child");
                rootElement.appendChild(childElement);
                
                qDebug() << domDocument.toString();
                
                &lt;?xml version=1.0?&gt;
                <root>
                    <child/>
                </root>    
                

                @

                1 Reply Last reply
                0
                • AlicemirrorA Offline
                  AlicemirrorA Offline
                  Alicemirror
                  wrote on last edited by
                  #8

                  @Lukas: in the example above it seems that the string is only to save the final xml file, is it true?

                  About this, then a pure QString can be used as source in a XmlListModel as you know ?

                  Enrico Miglino (aka Alicemirror)
                  Balearic Dynamics
                  Islas Baleares, Ibiza (Spain)
                  www.balearicdynamics.com

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    [quote author="Alicemirror" date="1316586696"]@Lukas: in the example above it seems that the string is only to save the final xml file, is it true?[/quote]

                    I assume you are referring to the example in your post as mine has been added later on.

                    To get the textual representation ("a XML file") of the in-memory DOM tree one might either use QDomDocument()::toString() or QDomNode()::save() (QDomDocument is-a QDomNode) which both should yield the same results.

                    The QString can be written to a file or passed to a XmlListModel (as .xml, not as .source).

                    1 Reply Last reply
                    0
                    • AlicemirrorA Offline
                      AlicemirrorA Offline
                      Alicemirror
                      wrote on last edited by
                      #10

                      :) Sure, the trick is the use of .xml instead of .source.

                      Many thanks for you clarifications. Hope it will be useful for Raijveer too.

                      Enrico Miglino (aka Alicemirror)
                      Balearic Dynamics
                      Islas Baleares, Ibiza (Spain)
                      www.balearicdynamics.com

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        It might be confusing that QDomDocument does not contain an XML declaration per default, but it is not required as per the XML standard. I've added a doc note on this to QDomDocument.

                        1 Reply Last reply
                        0
                        • AlicemirrorA Offline
                          AlicemirrorA Offline
                          Alicemirror
                          wrote on last edited by
                          #12

                          Meny thanks Lukas !

                          Enrico Miglino (aka Alicemirror)
                          Balearic Dynamics
                          Islas Baleares, Ibiza (Spain)
                          www.balearicdynamics.com

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            A complete example to create an XML document including the XML declaration as a processing instruction follows:

                            @
                            QDomDocument dom;
                            QDomProcessingInstruction xmlHeaderPI = dom.createProcessingInstruction("xml", "version="1.0" " );
                            dom.appendChild(xmlHeaderPI);
                            QDomElement root = dom.createElement("MyRoot");
                            dom.appendChild(root);
                            @

                            Also, be aware of the implications induced by the toString() method of QDomNode (and thus QDomDocument). It returns the text representation as UTF-8 string, although you might not want it encoded that way. The most reliable way is to use the method

                            @
                            QDomNode::save ( QTextStream & str, int indent, EncodingPolicy encodingPolicy )
                            @

                            It respects the encoding settings. You might wan to use a QBuffer or the like as the "device" for the text stream.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • AlicemirrorA Offline
                              AlicemirrorA Offline
                              Alicemirror
                              wrote on last edited by
                              #14

                              @Volker: Thanks you, very very useful. I am just working with a multilanguage aplication including Turkis.

                              Enrico Miglino (aka Alicemirror)
                              Balearic Dynamics
                              Islas Baleares, Ibiza (Spain)
                              www.balearicdynamics.com

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved