Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_xml_dom_qdom.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 <iostream>
4#include <cstring>
5#include <QFile>
6#include <QDomDocument>
7#include <QDomImplementation>
8
9void NodeElements();
10void DomText();
12void FileContent();
13void DocAppend();
14void XML_snippet_main();
15
16using namespace std;
19{
20QDomDocument doc;
22// This will create the element, but the resulting XML document will
23// be invalid, because '~' is not a valid character in a tag name.
25QDomElement elt1 = doc.createElement("foo~bar");
26
27// This will create an element with the tag name "foobar".
29QDomElement elt2 = doc.createElement("foo~bar");
30
31// This will create a null element.
33QDomElement elt3 = doc.createElement("foo~bar");
34}
36
38{
41QString someXML;
42
43d.setContent(someXML);
45while (!n.isNull()) {
46 if (n.isElement()) {
48 cout << "Element name: " << qPrintable(e.tagName()) << '\n';
49 break;
50 }
51 n = n.nextSibling();
52}
54
56QDomDocument document;
57QDomElement element1 = document.documentElement();
58QDomElement element2 = element1;
60
62QDomElement element3 = document.createElement("MyElement");
63QDomElement element4 = document.createElement("MyElement");
65
68//...
69QDomAttr a = e.attributeNode("href");
70cout << qPrintable(a.value()) << '\n'; // prints "http://qt-project.org"
71a.setValue("http://qt-project.org/doc"); // change the node's attribute
72QDomAttr a2 = e.attributeNode("href");
73cout << qPrintable(a2.value()) << '\n'; // prints "http://qt-project.org/doc"
75}
76
77void DomText()
78{
79QDomDocument doc;
82QDomElement element = doc.documentElement();
83for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
84{
85 QDomText t = n.toText();
86 if (!t.isNull())
87 text += t.data();
88}
90
91}
92
94{
96QDomDocument doc("mydocument");
97QFile file("mydocument.xml");
99 return;
100if (!doc.setContent(&file)) {
101 file.close();
102 return;
103}
104file.close();
105
106// print out the element names of all elements that are direct children
107// of the outermost element.
108QDomElement docElem = doc.documentElement();
109
110QDomNode n = docElem.firstChild();
111while(!n.isNull()) {
112 QDomElement e = n.toElement(); // try to convert the node to an element.
113 if(!e.isNull()) {
114 cout << qPrintable(e.tagName()) << '\n'; // the node really is an element.
115 }
116 n = n.nextSibling();
117}
118
119// Here we append a new element to the end of the document
120QDomElement elem = doc.createElement("img");
121elem.setAttribute("src", "myimage.png");
122docElem.appendChild(elem);
124}
125
127{
129QDomDocument doc;
130QDomElement root = doc.createElement("MyML");
131doc.appendChild(root);
132
133QDomElement tag = doc.createElement("Greeting");
134root.appendChild(tag);
135
136QDomText t = doc.createTextNode("Hello World");
137tag.appendChild(t);
138
139QString xml = doc.toString();
141}
\reentrant
Definition qdom.h:444
\reentrant
Definition qdom.h:269
QDomText createTextNode(const QString &data)
Creates a text node for the string value that can be inserted into the document tree,...
Definition qdom.cpp:6534
Q_WEAK_OVERLOAD ParseResult setContent(const QByteArray &data, ParseOptions options=ParseOption::Default)
Definition qdom.h:338
QDomElement documentElement() const
Returns the root element of the document.
Definition qdom.cpp:6488
QDomElement createElement(const QString &tagName)
Creates a new element called tagName that can be inserted into the DOM tree, e.g.
Definition qdom.cpp:6505
QString toString(int indent=1) const
Converts the parsed document back to its textual representation.
Definition qdom.cpp:6440
\reentrant
Definition qdom.h:471
QDomAttr attributeNode(const QString &name)
Returns the QDomAttr object that corresponds to the attribute called name.
Definition qdom.cpp:4390
void setAttribute(const QString &name, const QString &value)
Adds an attribute called name with value value.
Definition qdom.cpp:4294
QString tagName() const
Returns the tag name of this element.
Definition qdom.cpp:4254
\reentrant
Definition qdom.h:60
static void setInvalidDataPolicy(InvalidDataPolicy policy)
Definition qdom.cpp:596
\reentrant
Definition qdom.h:89
QDomNode firstChild() const
Returns the first child of the node.
Definition qdom.cpp:1712
QDomElement toElement() const
Converts a QDomNode into a QDomElement.
Definition qdom.cpp:6870
bool isNull() const
Returns true if this node is null (i.e.
Definition qdom.cpp:2087
QDomText toText() const
Converts a QDomNode into a QDomText.
Definition qdom.cpp:6896
QDomNode appendChild(const QDomNode &newChild)
Appends newChild as the node's last child.
Definition qdom.cpp:2063
\reentrant
Definition qdom.h:529
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1240
QString text
AudioChannelLayoutTag tag
GLboolean GLboolean GLboolean GLboolean a
[7]
GLfloat n
GLdouble GLdouble t
Definition qopenglext.h:243
#define qPrintable(string)
Definition qstring.h:1531
#define a2
QFile file
[0]
QXmlStreamReader xml
[0]
void XML_snippet_main()
[0]
void DocAppend()
void DomText()
void FirstElement()
void FileContent()
void NodeElements()
[0]