1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <qstringlist.h>
28 QDomDocument
* XMLWork::openFile(const QString
&name
, const QString
&filePath
)
30 QDomDocument
*doc
= new QDomDocument(name
);
32 if ( ! file
.open(QIODevice::ReadOnly
) ) {
33 // QMessageBox::information(this, "Load an XML file", "Error : un-openable file");
37 if ( ! doc
->setContent(&file
) ) {
38 // QMessageBox::information(this, "Load an XML file", "Error : malformed content");
47 QDomElement
XMLWork::getElement(const QDomElement
&startElement
, const QString
&elementPath
)
49 QStringList elements
= QStringList::split("/", elementPath
, false);
50 QDomNode n
= startElement
.firstChild();
51 for (unsigned int i
= 0; i
< elements
.count(); ++i
) { // For each elements
52 while ( ! n
.isNull() ) { // Browse theire sub elements
53 QDomElement e
= n
.toElement(); // and search the good one
54 if ( (!e
.isNull()) && e
.tagName() == *elements
.at(i
) ) { // If found
55 if ( i
+ 1 == elements
.count() ) // And if it is the asked element
56 return e
; // Return the first corresponding
57 else { // Or if it is an intermediate element
58 n
= e
.firstChild(); // Continue with the next sub element
65 return QDomElement(); // Not found !
68 QString
XMLWork::getElementText(const QDomElement
&startElement
, const QString
&elementPath
, const QString
&defaultTxt
)
70 QDomElement e
= getElement(startElement
, elementPath
);
77 void XMLWork::addElement(QDomDocument
&document
, QDomElement
&parent
, const QString
&name
, const QString
&text
)
79 QDomElement tag
= document
.createElement(name
);
80 parent
.appendChild(tag
);
81 QDomText content
= document
.createTextNode(text
);
82 tag
.appendChild(content
);
85 bool XMLWork::trueOrFalse(const QString
&value
, bool defaultValue
)
87 if ( value
== "true" || value
== "1" || value
== "on" || value
== "yes" )
89 if ( value
== "false" || value
== "0" || value
== "off" || value
== "no" )
94 QString
XMLWork::trueOrFalse(bool value
)
96 return value
? "true" : "false";
99 QString
XMLWork::innerXml(QDomElement
&element
)
102 for (QDomNode n
= element
.firstChild(); !n
.isNull(); n
= n
.nextSibling())
103 if (n
.isCharacterData())
104 inner
+= n
.toCharacterData().data();
105 else if (n
.isElement()) {
106 QDomElement e
= n
.toElement();
107 inner
+= "<" + e
.tagName() + ">" + innerXml(e
) + "</" + e
.tagName() + ">";