QT += xmlpatterns
aタグのname属性を取得する場合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1" ?> | |
<a name="tagName"></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
結果:
"<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
"
0 件のコメント:
コメントを投稿