PDA

View Full Version : QDomElement's text() for <element> </element> giving me a empty Qstring



CirqueForge
22nd April 2012, 03:11
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:


...
<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-interest/2001-11/thread01101-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?

norobro
22nd April 2012, 05:42
From the docs (link (http://doc-snapshot.qt-project.org/4.8/qdomdocument.html#setContent)):
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.

CirqueForge
22nd April 2012, 06:09
From the docs (link (http://doc-snapshot.qt-project.org/4.8/qdomdocument.html#setContent)):

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.


void ScfEncoder::load(QString path)
{
QFile structFile(path);

if(!structFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
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?

norobro
22nd April 2012, 06:49
Here's a simple example that hopefully will help :
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>

int main(){

QString xmlString("<original> </original>");
QXmlInputSource source;
source.setData(xmlString);
QXmlSimpleReader reader;
QDomDocument doc;
doc.setContent(&source, &reader);
QDomElement docElem = doc.documentElement();
qDebug() << docElem.tagName() << docElem.text();
}
Outputs: "original" " "

CirqueForge
22nd April 2012, 07:03
Here's a simple example that hopefully will help :
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>

int main(){

QString xmlString("<original> </original>");
QXmlInputSource source;
source.setData(xmlString);
QXmlSimpleReader reader;
QDomDocument doc;
doc.setContent(&source, &reader);
QDomElement docElem = doc.documentElement();
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.

norobro
22nd April 2012, 07:43
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.

CirqueForge
22nd April 2012, 14:42
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?

norobro
22nd April 2012, 23:09
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:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>

int main(){
QString xmlString("<original> </original>");
QDomDocument doc;
doc.setContent(xmlString);
QDomElement docElem = doc.documentElement();
qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
doc.clear();
QXmlInputSource source;
source.setData(xmlString);
QXmlSimpleReader reader;
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

CirqueForge
23rd April 2012, 12:55
Bingo.

Modified simple program:
#include <QDomDocument>
#include <QXmlSimpleReader>
#include <QDebug>

int main(){
QString xmlString("<original> </original>");
QDomDocument doc;
doc.setContent(xmlString);
QDomElement docElem = doc.documentElement();
qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
doc.clear();
QXmlInputSource source;
source.setData(xmlString);
QXmlSimpleReader reader;
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.

wysota
23rd April 2012, 13:53
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.