[Qt-interest] How to get XML as evaluation result of a QXmlQuery (XPath)?

Ritter, Nils Nils.Ritter at medizin.uni-leipzig.de
Mon Jul 11 11:30:39 CEST 2011


Hello,

I'm trying to query a XML file with QXmlQuery and XPath. Due to the structure of the XML it would be nice to get the result of the query as another XML string for further querying. My example XML string looks like this:



<legs>

                <left>

                               <leg>

                                               <name>Name1</name>

                               </leg>

                               <leg>

                                               <name>Name2</name>

                               </leg>

                               <leg>

                                               <name>Name3</name>

                               </leg>

                </left>

</legs>



The code to access the XML:

QCoreApplication a(argc, argv);

QString xml = "<legs><left><leg><name>Name1</name></leg><leg><name>Name1</name></leg>\

                               <leg><name>Name1</name></leg></left></legs>";

QBuffer device;

device.setData(xml.toUtf8());

device.open(QIODevice::ReadOnly);

QXmlQuery query;

query.bindVariable("inputDocument", &device); query.setQuery("doc($inputDocument)/legs/left/node()");

if (query.isValid()) {

    QStringList sl;

    query.evaluateTo(&sl);

    for (QStringList::iterator it = sl.begin(); it != sl.end(); ++it)

        qDebug() << *it;

} else

    qDebug() << "query invalid";



I tried several queries to access the XML of one leg:

/legs/left/node() returns empty string list

/legs/left/node()/string() returns three strings "Name1","Name2", "Name3" without any tags /legs/left return empty string list

/legs/left/string() returns just 3 strings containing "Name1","Name2", "Name3"



Is it possible to get XML as result of a QXmlQuery? If so, does work with evaluation to QStringList or are other structures necessary?



Thanks in advance.



-Nils

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110711/97a4d049/attachment.html 


More information about the Qt-interest-old mailing list