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. xml write with only space or empty issue
QtWS25 Last Chance

xml write with only space or empty issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
qxmlqdomxml parsingqt4.8.4xml
7 Posts 4 Posters 4.0k 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.
  • N Offline
    N Offline
    Narthan
    wrote on last edited by
    #1

    Hello all,

    i am trying write xml file using Qdom
    sample of xml file will be lke this
    <?xml version='1.0'?>
    <xml>
    <Config>
    <test>
    <tag1>edit1</tag1>
    <tag2>5996</tag2>
    </test>
    </Config>
    </xml>

    code sample:

            QByteArray xmlData(file->readAll());
            QDomDocument doc("config");
            doc.setContent(xmlData);
            QDomElement root = doc.firstChildElement("xml");
            QDomElement parent = root.firstChildElement("Config");
            QDomElement parent1 = parent.firstChildElement("test");
            QDomElement hostname = parent1.firstChildElement("tag1");
            hostname.firstChild().setNodeValue(ui->tag1_lineEdit->text());
            QDomElement username = parent1.firstChildElement("tag2");
            username.firstChild().setNodeValue(ui->tag2_lineEdit->text());
    

    if tag1_lineEdit contains only space (" ") or empty ("") then xml file will be look
    <?xml version='1.0'?>
    <xml>
    <Config>
    <test>
    <tag1/>
    <tag2>5996</tag2>
    </test>
    </Config>
    </xml>
    again i am not able write anything in that tag from lineedit, can someone give me solution, how to write space or empty text,

    RatzzR 1 Reply Last reply
    0
    • N Narthan

      Hello all,

      i am trying write xml file using Qdom
      sample of xml file will be lke this
      <?xml version='1.0'?>
      <xml>
      <Config>
      <test>
      <tag1>edit1</tag1>
      <tag2>5996</tag2>
      </test>
      </Config>
      </xml>

      code sample:

              QByteArray xmlData(file->readAll());
              QDomDocument doc("config");
              doc.setContent(xmlData);
              QDomElement root = doc.firstChildElement("xml");
              QDomElement parent = root.firstChildElement("Config");
              QDomElement parent1 = parent.firstChildElement("test");
              QDomElement hostname = parent1.firstChildElement("tag1");
              hostname.firstChild().setNodeValue(ui->tag1_lineEdit->text());
              QDomElement username = parent1.firstChildElement("tag2");
              username.firstChild().setNodeValue(ui->tag2_lineEdit->text());
      

      if tag1_lineEdit contains only space (" ") or empty ("") then xml file will be look
      <?xml version='1.0'?>
      <xml>
      <Config>
      <test>
      <tag1/>
      <tag2>5996</tag2>
      </test>
      </Config>
      </xml>
      again i am not able write anything in that tag from lineedit, can someone give me solution, how to write space or empty text,

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      Hi,
      <tag1/> indicates that it is empty

      <tag1/> is same as <tag1></tag1>
      

      http://stackoverflow.com/questions/7238254/xml-what-is-this-null-or-empty-element

      --Alles ist gut.

      N 1 Reply Last reply
      0
      • RatzzR Ratzz

        Hi,
        <tag1/> indicates that it is empty

        <tag1/> is same as <tag1></tag1>
        

        http://stackoverflow.com/questions/7238254/xml-what-is-this-null-or-empty-element

        N Offline
        N Offline
        Narthan
        wrote on last edited by
        #3

        @Ratzz Thank you for your reply, but again i am not able to write in the tag.,
        For example based on save button i am saving xml file, once i entered only space to line edit, then i enter save button, it will be a empty tag(<tag1/>) in xml file, afterward again i enetered some value to lineedit and entered save button , on that time it is not updating in empty tag,

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          from http://doc.qt.io/qt-5/qdomdocument.html

          "Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument. If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be supplied."

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Paul ColbyP 1 Reply Last reply
          1
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            It works for me (just replacing your lineEdit with plain text for testing...

            QByteArray xmlData("<?xml version='1.0'?><xml><Config><test><tag1>edit1"
                               "</tag1><tag2>5996</tag2></test></Config></xml>");
            QDomDocument doc("config");
            doc.setContent(xmlData);
            QDomElement root = doc.firstChildElement("xml");
            QDomElement parent = root.firstChildElement("Config");
            QDomElement parent1 = parent.firstChildElement("test");
            QDomElement hostname = parent1.firstChildElement("tag1");
            hostname.firstChild().setNodeValue(" "); // One space.
            QDomElement username = parent1.firstChildElement("tag2");
            username.firstChild().setNodeValue(""); // Empty string.
            qDebug() << doc.toString();
            
            "<?xml version='1.0'?>\n<xml>\n <Config>\n  <test>\n   <tag1> </tag1>\n   <tag2></tag2>\n  </test>\n </Config>\n</xml>\n"
            

            How are you printing the document? And what version of Qt are you using?

            Cheers.

            N 1 Reply Last reply
            0
            • VRoninV VRonin

              from http://doc.qt.io/qt-5/qdomdocument.html

              "Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument. If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be supplied."

              Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #6

              @VRonin said:

              from http://doc.qt.io/qt-5/qdomdocument.html

              "Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument. If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be supplied."

              Oh, so its problem with QDomDocument::setContent, eg:

              QByteArray xmlData("<?xml version='1.0'?><xml><Config><test><tag1>edit1"
                                 "</tag1><tag2>5996</tag2></test></Config></xml>");
              QDomDocument doc("config");
              doc.setContent(xmlData);
              QDomElement root = doc.firstChildElement("xml");
              QDomElement parent = root.firstChildElement("Config");
              QDomElement parent1 = parent.firstChildElement("test");
              QDomElement hostname = parent1.firstChildElement("tag1");
              hostname.firstChild().setNodeValue(" "); // One space.
              QDomElement username = parent1.firstChildElement("tag2");
              username.firstChild().setNodeValue(""); // Empty string.
              qDebug() << doc.toString();
              
              QDomDocument doc2;
              doc2.setContent(doc.toString());
              qDebug() << doc2.toString();
              

              Output:

              "<?xml version='1.0'?>\n<xml>\n <Config>\n  <test>\n   <tag1> </tag1>\n   <tag2></tag2>\n  </test>\n </Config>\n</xml>\n"
              "<?xml version='1.0'?>\n<xml>\n <Config>\n  <test>\n   <tag1/>\n   <tag2/>\n  </test>\n </Config>\n</xml>\n"
              

              So the empty space is added correctly, even written to string correctly (doc). It just disappears when its parsed back in again (doc2).

              Cheers.

              1 Reply Last reply
              1
              • Paul ColbyP Paul Colby

                It works for me (just replacing your lineEdit with plain text for testing...

                QByteArray xmlData("<?xml version='1.0'?><xml><Config><test><tag1>edit1"
                                   "</tag1><tag2>5996</tag2></test></Config></xml>");
                QDomDocument doc("config");
                doc.setContent(xmlData);
                QDomElement root = doc.firstChildElement("xml");
                QDomElement parent = root.firstChildElement("Config");
                QDomElement parent1 = parent.firstChildElement("test");
                QDomElement hostname = parent1.firstChildElement("tag1");
                hostname.firstChild().setNodeValue(" "); // One space.
                QDomElement username = parent1.firstChildElement("tag2");
                username.firstChild().setNodeValue(""); // Empty string.
                qDebug() << doc.toString();
                
                "<?xml version='1.0'?>\n<xml>\n <Config>\n  <test>\n   <tag1> </tag1>\n   <tag2></tag2>\n  </test>\n </Config>\n</xml>\n"
                

                How are you printing the document? And what version of Qt are you using?

                Cheers.

                N Offline
                N Offline
                Narthan
                wrote on last edited by
                #7

                @Paul-Colby i am opening xml file as you mentioned
                first time when i pressed save button it will show
                <?xml version='1.0'?>
                <xml>
                <tag1> </tag1> // two spaces from line edit
                </xml>

                second time when i presses save button it will show

                <?xml version='1.0'?>
                <xml>
                <tag1/> // some data in lineedit
                </xml>

                third time when i presses save button it will show

                <?xml version='1.0'?>
                <xml>
                <tag1/> // some data in lineedit
                </xml>

                i am using Qt 4.8.7, i am opening xml file , see the result

                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