Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
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.
18 "xml", "version=\"1.0\" encoding=\"utf-8\"");
19 document->appendChild(process);
20
21 QDomElement documentElement = document->createElement("document");
22 document->appendChild(documentElement);
23
25 QTextBlock currentBlock = textDocument->begin();
26
27 while (currentBlock.isValid()) {
29 QDomElement blockElement = document->createElement("block");
30 document->appendChild(blockElement);
31
32 readFragment(currentBlock, blockElement, document);
33
35 processBlock(currentBlock);
37
39 currentBlock = currentBlock.next();
40 }
42
43 return document;
44}
45
46void XmlWriter::readFragment(const QTextBlock &currentBlock,
47 QDomElement blockElement,
48 QDomDocument *document)
49{
52 for (it = currentBlock.begin(); !(it.atEnd()); ++it) {
53 QTextFragment currentFragment = it.fragment();
54 if (currentFragment.isValid())
56 processFragment(currentFragment);
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 }
69 }
71}
72
73void XmlWriter::processBlock(const QTextBlock &currentBlock)
74{
75 // Dummy use of specified parameter currentBlock
76 QTextBlock localBlock;
77 localBlock = currentBlock;
78
79}
80
81void XmlWriter::processFragment(const QTextFragment &currentFragment)
82{
83 // Dummy use of specified parameter currentFragment
84 QTextFragment localFragment;
85 localFragment = currentFragment;
86}
\reentrant
Definition qdom.h:240
\reentrant
Definition qdom.h:266
QDomProcessingInstruction createProcessingInstruction(const QString &target, const QString &data)
Creates a new processing instruction that can be inserted into the document, e.g.
Definition qdom.cpp:6584
QDomElement createElement(const QString &tagName)
Creates a new element called tagName that can be inserted into the DOM tree, e.g.
Definition qdom.cpp:6503
\reentrant
Definition qdom.h:468
void setAttribute(const QString &name, const QString &value)
Adds an attribute called name with value value.
Definition qdom.cpp:4295
\reentrant
Definition qdom.h:58
QDomDocumentType createDocumentType(const QString &qName, const QString &publicId, const QString &systemId)
Creates a document type node for the name qName.
Definition qdom.cpp:486
QDomNode appendChild(const QDomNode &newChild)
Appends newChild as the node's last child.
Definition qdom.cpp:2065
\reentrant
Definition qdom.h:526
\reentrant
iterator begin() const
Returns a text block iterator pointing to the beginning of the text block.
bool isValid() const
Returns true if this text block is valid; otherwise returns false.
QTextBlock next() const
Returns the text block in the document after this block, or an empty text block if this is the last o...
QTextBlock begin() const
Returns the document's first text block.
\reentrant
QString text() const
Returns the text fragment's as plain text.
bool isValid() const
Returns true if this is a valid text fragment (i.e.
int length() const
Returns the number of characters in the text fragment.
QDomDocument * toXml()
Definition xmlwriter.cpp:7
QSet< QString >::iterator it