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
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
9
void
NodeElements
();
10
void
DomText
();
11
void
FirstElement
();
12
void
FileContent
();
13
void
DocAppend
();
14
void
XML_snippet_main
();
15
16
using
namespace
std
;
17
//! [0]
18
void
XML_snippet_main
()
19
{
20
QDomDocument doc;
21
QDomImplementation impl;
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.
24
impl.setInvalidDataPolicy(QDomImplementation::AcceptInvalidChars);
25
QDomElement elt1 = doc.createElement(
"foo~bar"
);
26
27
// This will create an element with the tag name "foobar".
28
impl.setInvalidDataPolicy(QDomImplementation::DropInvalidChars);
29
QDomElement elt2 = doc.createElement(
"foo~bar"
);
30
31
// This will create a null element.
32
impl.setInvalidDataPolicy(QDomImplementation::ReturnNullNode);
33
QDomElement elt3 = doc.createElement(
"foo~bar"
);
34
}
35
//! [0]
36
37
void
NodeElements
()
38
{
39
//! [1]
40
QDomDocument d;
41
QString someXML;
42
43
d.setContent(someXML);
44
QDomNode n = d.firstChild();
45
while
(!n.isNull()) {
46
if
(n.isElement()) {
47
QDomElement e = n.toElement();
48
cout <<
"Element name: "
<< qPrintable(e.tagName()) <<
'\n'
;
49
break
;
50
}
51
n = n.nextSibling();
52
}
53
//! [1]
54
55
//! [2]
56
QDomDocument document;
57
QDomElement element1 = document.documentElement();
58
QDomElement element2 = element1;
59
//! [2]
60
61
//! [3]
62
QDomElement element3 = document.createElement(
"MyElement"
);
63
QDomElement element4 = document.createElement(
"MyElement"
);
64
//! [3]
65
66
//! [8]
67
QDomElement e;
68
//...
69
QDomAttr a = e.attributeNode(
"href"
);
70
cout << qPrintable(a.value()) <<
'\n'
;
// prints "http://qt-project.org"
71
a.setValue(
"http://qt-project.org/doc"
);
// change the node's attribute
72
QDomAttr a2 = e.attributeNode(
"href"
);
73
cout << qPrintable(a2.value()) <<
'\n'
;
// prints "http://qt-project.org/doc"
74
//! [8]
75
}
76
77
void
DomText
()
78
{
79
QDomDocument doc;
80
//! [10]
81
QString text;
82
QDomElement element = doc.documentElement();
83
for
(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
84
{
85
QDomText t = n.toText();
86
if
(!t.isNull())
87
text += t.data();
88
}
89
//! [10]
90
91
}
92
93
void
FileContent
()
94
{
95
//! [16]
96
QDomDocument doc(
"mydocument"
);
97
QFile file(
"mydocument.xml"
);
98
if
(!file.open(QIODevice::ReadOnly))
99
return
;
100
if
(!doc.setContent(&file)) {
101
file.close();
102
return
;
103
}
104
file.close();
105
106
// print out the element names of all elements that are direct children
107
// of the outermost element.
108
QDomElement docElem = doc.documentElement();
109
110
QDomNode n = docElem.firstChild();
111
while
(!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
120
QDomElement elem = doc.createElement(
"img"
);
121
elem.setAttribute(
"src"
,
"myimage.png"
);
122
docElem.appendChild(elem);
123
//! [16]
124
}
125
126
void
DocAppend
()
127
{
128
//! [17]
129
QDomDocument doc;
130
QDomElement root = doc.createElement(
"MyML"
);
131
doc.appendChild(root);
132
133
QDomElement tag = doc.createElement(
"Greeting"
);
134
root.appendChild(tag);
135
136
QDomText t = doc.createTextNode(
"Hello World"
);
137
tag.appendChild(t);
138
139
QString xml = doc.toString();
140
//! [17]
141
}
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
XML_snippet_main
void XML_snippet_main()
[0]
Definition
src_xml_dom_qdom.cpp:18
DocAppend
void DocAppend()
Definition
src_xml_dom_qdom.cpp:126
DomText
void DomText()
Definition
src_xml_dom_qdom.cpp:77
FirstElement
void FirstElement()
FileContent
void FileContent()
Definition
src_xml_dom_qdom.cpp:93
NodeElements
void NodeElements()
[0]
Definition
src_xml_dom_qdom.cpp:37
qtbase
src
xml
doc
snippets
code
src_xml_dom_qdom.cpp
Generated on
for Qt by
1.18.0