Writing a little code to extract some values from an XML, the result of the XPath seem to add \n
after the content.
#include <QCoreApplication>
#include <QXmlQuery>
#include <QString>
#include <QDebug>
auto main(int argn, char* argc[])->int
{
QCoreApplication app(argn, argc);
QString replyContent="<Root isOk='ok'/>";
QXmlQuery query;
query.setFocus(replyContent);
query.setQuery("string(//@isOk)");
// Attribute seem to add \n
QString queryResult;
if (query.evaluateTo(&queryResult))
{
qDebug() << queryResult; // Where this \n come from?
qDebug() << queryResult.size(); // Why 3? shouldn't be 2?
}
}
Expected result:
"ok"
2
Given result:
"ok\n"
3
This obviously has some side effects which I would like to avoid.
Why is this \n
added? And how to solve it?
I think that this is introduced by the QXmlFormatter
that is used when serialising the results to a QString
; I suspect that QXmlFormatter::endDocument
writes a newline.
One workaround would be to output to a string list instead, then pick the first element:
QStringList results;
if (query.evaluateTo(&results))
{
const QString& queryResult = results.first();
qDebug() << queryResult;
qDebug() << queryResult.size();
}
You might choose to join()
the results instead, if you need them all.