Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved xml write with only space or empty issue

    General and Desktop
    qxml qdom xml parsing qt4.8.4 xml
    4
    7
    3261
    Loading More Posts
    • 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
      Narthan last edited by

      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,

      Ratzz 1 Reply Last reply Reply Quote 0
      • Ratzz
        Ratzz @Narthan last edited by

        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 Reply Quote 0
        • N
          Narthan @Ratzz last edited by

          @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 Reply Quote 0
          • VRonin
            VRonin last edited by

            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 Colby 1 Reply Last reply Reply Quote 1
            • Paul Colby
              Paul Colby last edited by

              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 Reply Quote 0
              • Paul Colby
                Paul Colby @VRonin last edited by

                @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 Reply Quote 1
                • N
                  Narthan @Paul Colby last edited by

                  @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 Reply Quote 0
                  • First post
                    Last post