QDomElement's text() for <element> </element> giving me a empty Qstring
Hello. I'm confronted with a problem reading .XML files with my Qt application. When I try and read a XML file that has something like the following with an element with whitespace:
Code:
...
<ctrl value="EW45" index="3"/>
<text index="4">
<original> </original>
</text>
...
The whitespace between "<original> </original>" isn't preserved and the resulting Qstring is empty. Unfortunately, this creates problems as I have several of these whitespace nodes in the XML files I need to use. I read up on the issue on http://lists.trolltech.com/qt-intere...ad01101-0.html and apprently, it's a really old unfixed "bug" or feature depending on how you look at it. My question is how am I suppose to read the node correctly so I can have these whitespace nodes give me a Qstring that isn't empty?
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
From the docs (link):
Quote:
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.
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
norobro
Edit: I'm lacking a bit of sleep on this so what I said previously in this post is kinda off.
Maybe I'm not getting the full picture with how to use that class to read whitespace, so could you explain it a bit more?
Right now, my QDomDocument is set as private in my header and predefined. This is the code I use to load the data for retrieving my data.
Code:
void ScfEncoder
::load(QString path
) {
{
fatalExit("Failed to open input file!");
}
/* Load all data */
xmlData.setContent(structFile.readAll());
structFile.close();
// Get root
root = xmlData.documentElement();
if(root.isNull())
{
fatalExit("Root of the input XML file was not detected!");
}
}
I'm just asking if there is any way to retrieve whitespace from a node such as the one I provided above using any existing methods or classes in Qt or do I have to kinda do it manually, if it's even possible?
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Here's a simple example that hopefully will help :
Code:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>
int main(){
QString xmlString
("<original> </original>");
source.setData(xmlString);
doc.setContent(&source, &reader);
qDebug() << docElem.tagName() << docElem.text();
}
Outputs: "original" " "
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
norobro
Here's a simple example that hopefully will help :
Code:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>
int main(){
QString xmlString
("<original> </original>");
source.setData(xmlString);
doc.setContent(&source, &reader);
qDebug() << docElem.tagName() << docElem.text();
}
Outputs: "original" " "
Kinda helpful for me to understand what QXmlSimpleReader does but I think you are misunderstanding me.
The thing is from "<original> </original>", I want to get a Qstring with " ". As in, I want to read the whitespace and nothing else. I want it so that not only when I parse "<original> </original>" do I get a whitespace but for some control tags, "<control>  </control>", the Qstring would give me " ". Is there a way from any classes in Qt or any kind of manual implementation I can use to read a node so it will do that for me? Or is there some way I can bypass this non-whitespace reading?
Edit: Even the forum doesn't seem to respect whitespace handling, haha... I meant for <control></control> to have 6 spaces in between.
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
CirqueForge
As in, I want to read the whitespace and nothing else.
QDomElement::text() will return a QString that will contain 6 spaces if your element contains 6 spaces.
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
norobro
QDomElement::text() will return a QString that will contain 6 spaces if your element contains 6 spaces.
It doesn't if the node is just whitespace. It will just return an empty Qstring. That's the entire problem and hence, why I'm asking if there's another way to get text from the element using either other classes or doing it manually.
Edit: Do you mean to say that when I read it using a QXMLSimpleReader, QDomElement::text() will return the whitespace nodes correctly?
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
CirqueForge
Do you mean to say that when I read it using a QXMLSimpleReader, QDomElement::text() will return the whitespace nodes correctly?
Bingo.
Modified simple program:
Code:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>
int main(){
QString xmlString
("<original> </original>");
doc.setContent(xmlString);
qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
doc.clear();
source.setData(xmlString);
doc.setContent(&source, &reader);
docElem = doc.documentElement();
qDebug() << "With QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
}
//Output:
//Without QXmlSimpleReader - element contains "" length= 0
//With QXmlSimpleReader - element contains " " length= 6
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
norobro
Bingo.
Modified simple program:
Code:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>
int main(){
QString xmlString
("<original> </original>");
doc.setContent(xmlString);
qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
doc.clear();
source.setData(xmlString);
doc.setContent(&source, &reader);
docElem = doc.documentElement();
qDebug() << "With QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
}
//Output:
//Without QXmlSimpleReader - element contains "" length= 0
//With QXmlSimpleReader - element contains " " length= 6
Yeah, I was able to get it to work correctly after doing so. Thank you for your help.
Re: QDomElement's text() for <element> </element> giving me a empty Qstring
Quote:
Originally Posted by
CirqueForge
Edit: Even the forum doesn't seem to respect whitespace handling, haha... I meant for <control></control> to have 6 spaces in between.
It's not the forum, it's your web browser.