Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
xmlwriter.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
#
include
"xmlwriter.h"
4
5
#
include
<
QTextDocument
>
6
7
QDomDocument
*XmlWriter::toXml()
8
{
9
QDomImplementation implementation;
10
QDomDocumentType docType = implementation.createDocumentType(
11
"scribe-document"
,
"scribe"
,
"qt.nokia.com/scribe"
);
12
13
document =
new
QDomDocument(docType);
14
15
// ### This processing instruction is required to ensure that any kind
16
// of encoding is given when the document is written.
17
QDomProcessingInstruction process = document->createProcessingInstruction(
18
"xml"
,
"version=\"1.0\" encoding=\"utf-8\""
);
19
document->appendChild(process);
20
21
QDomElement documentElement = document->createElement(
"document"
);
22
document->appendChild(documentElement);
23
24
//! [0]
25
QTextBlock currentBlock = textDocument->begin();
26
27
while
(currentBlock.isValid()) {
28
//! [0]
29
QDomElement blockElement = document->createElement(
"block"
);
30
document->appendChild(blockElement);
31
32
readFragment(currentBlock, blockElement, document);
33
34
//! [1]
35
processBlock(currentBlock);
36
//! [1]
37
38
//! [2]
39
currentBlock = currentBlock.next();
40
}
41
//! [2]
42
43
return
document;
44
}
45
46
void
XmlWriter
::readFragment(
const
QTextBlock ¤tBlock,
47
QDomElement blockElement,
48
QDomDocument *document)
49
{
50
//! [3] //! [4]
51
QTextBlock::iterator it;
52
for
(it = currentBlock.begin(); !(it.atEnd()); ++it) {
53
QTextFragment currentFragment = it.fragment();
54
if
(currentFragment.isValid())
55
//! [3] //! [5]
56
processFragment(currentFragment);
57
//! [4] //! [5]
58
59
if
(currentFragment.isValid()) {
60
QDomElement fragmentElement = document->createElement(
"fragment"
);
61
blockElement.appendChild(fragmentElement);
62
63
fragmentElement.setAttribute(
"length"
, currentFragment.length());
64
QDomText fragmentText = document->createTextNode(currentFragment.text());
65
66
fragmentElement.appendChild(fragmentText);
67
}
68
//! [6] //! [7]
69
}
70
//! [7] //! [6]
71
}
72
73
void
XmlWriter
::processBlock(
const
QTextBlock ¤tBlock)
74
{
75
// Dummy use of specified parameter currentBlock
76
QTextBlock localBlock;
77
localBlock = currentBlock;
78
79
}
80
81
void
XmlWriter
::processFragment(
const
QTextFragment ¤tFragment)
82
{
83
// Dummy use of specified parameter currentFragment
84
QTextFragment localFragment;
85
localFragment = currentFragment;
86
}
XmlWriter
Definition
xmlwriter.h:15
QT_FORWARD_DECLARE_CLASS
QT_FORWARD_DECLARE_CLASS(QDomDocument)
qtbase
src
gui
doc
snippets
textblock-fragments
xmlwriter.cpp
Generated on
for Qt by
1.18.0