ページ

2014年1月27日月曜日

QXmlQuery を使ってみる :Qt

.pro ファイルに追記
QT += xmlpatterns

aタグのname属性を取得する場合

<?xml version="1.0" encoding="ISO-8859-1" ?>
<a name="tagName"></a>
view raw data.xml hosted with ❤ by GitHub
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QXmlQuery>
#include <QXmlResultItems>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Open a file
QString filename( "D:\\soft\\pleiades\\xampp\\htdocs\\www\\data.xml" );
QFile file(filename);
file.open(QIODevice::ReadOnly);
// Query
QXmlQuery query;
query.bindVariable("fileName", &file);
query.setQuery("declare variable $fileName external; doc($fileName)/a/@name");
// or
// query.setQuery("declare variable $fileName external; doc($fileName)/a/@name/data(.)");
// Result
QXmlResultItems xmlResultItems;
query.evaluateTo(&xmlResultItems);
// Try to get the first item
QXmlItem item(xmlResultItems.next());
while(!item.isNull())
{
if(item.isAtomicValue())
{
// Output for the query "/a/@name/data(.)"
qDebug() << "Atomic value : " << item.toAtomicValue().toString();
}
else if(item.isNode())
{
// Output for the query "/a/@name"
qDebug() << "Node : " << item.toNodeModelIndex().stringValue();
}
else
{
// Item must be null
}
// Next item
item = xmlResultItems.next();
}
// Close the file
file.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
view raw mainwindow.cpp hosted with ❤ by GitHub
結果: Node : "tagName"

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
view raw data2.xml hosted with ❤ by GitHub
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QXmlQuery>
#include <QXmlResultItems>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Open a file
QString filename( "D:\\soft\\pleiades\\xampp\\htdocs\\www\\data.xml" );
QFile file(filename);
file.open(QIODevice::ReadOnly);
// Query
QXmlQuery query;
query.bindVariable("fileName", &file);
query.setQuery("doc($fileName)/bookstore/book[price>30]/title");
QString testOutput;
query.evaluateTo(&testOutput);
qDebug() << testOutput;
// Close the file
file.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
view raw mainwindow2.cpp hosted with ❤ by GitHub

結果:
"<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
"

0 件のコメント:

コメントを投稿