-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLReader.cpp
More file actions
140 lines (130 loc) · 5.24 KB
/
XMLReader.cpp
File metadata and controls
140 lines (130 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
* Copyright (C) 2023 by Viktor Dózsa *
* luke2135@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 3 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses>. *
***************************************************************************/
//#include "ui_FC3MMwidget.h" //https://forum.qt.io/topic/20570/qtcreator-ui-designer-suddenly-fails-to-add-find-slots/5
#include <XMLReader.h>
#include <QDebug>
#include <QObject>
/****************************************************************************************************
* An Element is something that does or could have attributes of its own or contain other elements. *
* An Attribute is something that is self-contained, i.e., a color, an ID, a name. *
* An element can contain: text, attributes, other elements, or a mix of the above. *
* Metadata (data about data) should be stored as attributes, and the data itself should be stored *
* as elements. https://www.w3schools.com/xml/xml_attributes.asp *
****************************************************************************************************/
XMLReader::XMLReader(QDomDocument * document)
{
if ( document == NULL )
{
str = "XMLReader constructor without document pointer. Exiting.";
//ui.textEdit->append( "XMLReader constructor without document pointer. Exiting." );
return;
}
}
// QString QDomNode::nodeName() const
// QDomNode::NodeType QDomNode::nodeType() const
// QString QDomNode::nodeValue() const
// https://stackoverflow.com/questions/49188597/how-to-parse-an-unknown-xml-in-qt-and-get-all-the-attributes-in-it
XMLReader::~XMLReader()
{
}
void XMLReader::ReadXMLTest(QDomDocument * document, QString fileName)
{
QFile file(fileName);
QMessageBox msgBox;
if ( !file.open( QIODevice::ReadOnly ) )
{
msgBox.setText( QObject::tr("Opening file %1 failed.").arg(fileName) );
msgBox.exec();
return;
}
if ( !document->setContent(&file) )
{
file.close();
msgBox.setText( QObject::tr("Parsing XML file %1 failed.").arg(fileName) );
msgBox.exec();
return;
}
file.close();
str = "XMLReader test - file opened and closed. Element names:\n";
//emit XMLSendStringBasic(str);
emit XMLStringPointerSender(&str);
// print out the element names of all elements that are direct children of the outermost element.
QDomElement docElem = document->documentElement(); // The root element is available using documentElement().
QDomNode docNode = docElem.firstChild();
while ( !docNode.isNull() )
{
QDomElement docElement = docNode.toElement(); // try to convert the node to an element.
if ( !docElement.isNull() )
{
QDomNamedNodeMap attribVar = docElement.attributes();
for ( int i = 0; i < attribVar.count(); ++i)
{
//qDebug() << "Attributes found: " << attribVar.item(i).nodeName();
str = "attribute name: " + attribVar.item(i).nodeName() + ", attribute value: " + attribVar.item(i).nodeValue();
//emit XMLSendStringBasic(str);
emit XMLStringPointerSender(&str);
}
str = "element name: " + docElement.tagName() + ", element text: " + docElement.text();
//qDebug() << str;
//emit XMLSendStringBasic(str);
emit XMLStringPointerSender(&str);
}
docNode = docNode.nextSibling();
}
}
void XMLReader::ReadXML(QDomDocument * document, QString fileName)
{
QFile file(fileName);
if ( !file.open( QIODevice::ReadOnly ) )
{
str = QObject::tr("Opening file %1 failed.").arg(fileName);
emit XMLStringPointerSender(&str);
return;
}
if ( !document->setContent(&file) )
{
file.close();
str = QObject::tr("Parsing XML file %1 failed.").arg(fileName);
emit XMLStringPointerSender(&str);
return;
}
file.close();
}
void XMLReader::WriteXML(QDomDocument * document, QString fileName)
{
qint64 qi64Written; //fSize
QFile file(fileName);
if ( !file.open( QIODevice::WriteOnly ) )
{
str = QObject::tr("Opening file %1 failed.").arg(fileName);
emit XMLStringPointerSender(&str);
return;
}
//fSize = file.size();
baData = document->toByteArray();
qi64Written = file.write(baData.data(), baData.size() ); //fSize + 500 = 500?!
if ( qi64Written > -1 )
{
str = QObject::tr( "%1 updated successfully." ).arg(file.fileName());
}
else
{
str = QObject::tr( "Error writing data to %1." ).arg(file.fileName());
}
emit XMLStringPointerSender(&str);
file.close();
}