Qt 5.10 Book Examples
chapter40
XmlSAXRead
main.cpp
Go to the documentation of this file.
1
// ======================================================================
2
// main.cpp
3
// ======================================================================
4
// This file is a part of the book
5
// "Qt 5.10 Professional programming with C++"
6
// http://qt-book.com
7
// ======================================================================
8
// Copyright (c) 2017 by Max Schlee
9
// ======================================================================
10
11
#include <QtXml>
12
13
// ======================================================================
14
class
AddressBookParser
:
public
QXmlDefaultHandler {
15
private
:
16
QString m_strText;
17
18
public
:
19
// ------------------------------------------------------------------
20
bool
startElement
(
const
QString&,
21
const
QString&,
22
const
QString&,
23
const
QXmlAttributes& attrs
24
)
25
{
26
for
(
int
i = 0; i < attrs.count(); i++) {
27
if
(attrs.localName(i) ==
"number"
) {
28
qDebug() <<
"Attr:"
<< attrs.value(i);
29
}
30
}
31
return
true
;
32
}
33
34
// ------------------------------------------------------------------
35
bool
characters
(
const
QString& strText)
36
{
37
m_strText = strText;
38
return
true
;
39
}
40
41
// ------------------------------------------------------------------
42
bool
endElement
(
const
QString&,
const
QString&,
const
QString& str)
43
{
44
45
if
(str !=
"contact"
&& str !=
"addressbook"
) {
46
qDebug() <<
"TagName:"
<< str
47
<<
"\tText:"
<< m_strText;
48
}
49
return
true
;
50
}
51
52
// ------------------------------------------------------------------
53
bool
fatalError
(
const
QXmlParseException& exception)
54
{
55
qDebug() <<
"Line:"
<< exception.lineNumber()
56
<<
", Column:"
<< exception.columnNumber()
57
<<
", Message:"
<< exception.message();
58
return
false
;
59
}
60
};
61
62
// ----------------------------------------------------------------------
63
int
main
()
64
{
65
AddressBookParser
handler;
66
QFile file(
"addressbook.xml"
);
67
QXmlInputSource source(&file);
68
QXmlSimpleReader reader;
69
70
reader.setContentHandler(&handler);
71
reader.parse(source);
72
73
return
0;
74
}
AddressBookParser
Definition:
main.cpp:14
AddressBookParser::endElement
bool endElement(const QString &, const QString &, const QString &str)
Definition:
main.cpp:42
main
int main(int argc, char **argv)
Definition:
main.cpp:15
AddressBookParser::characters
bool characters(const QString &strText)
Definition:
main.cpp:35
AddressBookParser::fatalError
bool fatalError(const QXmlParseException &exception)
Definition:
main.cpp:53
AddressBookParser::startElement
bool startElement(const QString &, const QString &, const QString &, const QXmlAttributes &attrs)
Definition:
main.cpp:20
Generated by
1.8.14