Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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;
17//! [0]
19{
20QDomDocument doc;
21QDomImplementation 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.
24impl.setInvalidDataPolicy(QDomImplementation::AcceptInvalidChars);
25QDomElement elt1 = doc.createElement("foo~bar");
26
27// This will create an element with the tag name "foobar".
28impl.setInvalidDataPolicy(QDomImplementation::DropInvalidChars);
29QDomElement elt2 = doc.createElement("foo~bar");
30
31// This will create a null element.
32impl.setInvalidDataPolicy(QDomImplementation::ReturnNullNode);
33QDomElement elt3 = doc.createElement("foo~bar");
34}
35//! [0]
36
38{
39//! [1]
40QDomDocument d;
41QString someXML;
42
43d.setContent(someXML);
44QDomNode n = d.firstChild();
45while (!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]
56QDomDocument document;
57QDomElement element1 = document.documentElement();
58QDomElement element2 = element1;
59//! [2]
60
61//! [3]
62QDomElement element3 = document.createElement("MyElement");
63QDomElement element4 = document.createElement("MyElement");
64//! [3]
65
66//! [8]
67QDomElement e;
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"
74//! [8]
75}
76
77void DomText()
78{
79QDomDocument doc;
80//! [10]
81QString text;
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}
89//! [10]
90
91}
92
94{
95//! [16]
96QDomDocument doc("mydocument");
97QFile file("mydocument.xml");
98if (!file.open(QIODevice::ReadOnly))
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);
123//! [16]
124}
125
127{
128//! [17]
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();
140//! [17]
141}
void XML_snippet_main()
[0]
void DocAppend()
void DomText()
void FirstElement()
void FileContent()
void NodeElements()
[0]