Небольшой примерчик как работать с QXmlQuery
Пример xml файла для парсинга:
[cce lang=»xml»]
< ?xml version="1.0" encoding="utf-8"?>
LOTS/contakt.png
…..
[/cc]
Парсинг файла
Добавим в файл проекта (*.pro)
[cce lang=»c++»]
QT += xmlpatterns
[/cc]
Ну и подключим либы:
[cce lang=»c++»]
#include
#include
#include
#include
[/cc]
[cce lang=»c++»]
QFile file(«data.xml»);
if (!file.open(QIODevice::ReadOnly)) {
qDebug()<<«file not opened!»;< return;
}
QXmlQuery xmlQuery;
xmlQuery.bindVariable("myDocument", &file);
xmlQuery.setQuery("doc($myDocument)/root/lot[2]/description/string()"); //-- если не указать /string() то вернётся xml с тэгами
if(!xmlQuery.isValid()) {
qDebug()<<"xml not valid, syka";
return ;
}
//-- выводим результат
QString res;
xmlQuery.evaluateTo(&res); //-- приводим результат к строке
qDebug()<<"RES:"<
QBuffer buffer( &byteArray );
buffer.open( QIODevice::ReadOnly );
xmlQuery.bindVariable(«myDocument», &buffer);
[/cc]