xml write with only space or empty issue
-
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 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, -
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."
-
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.
-
@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.
-
@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