glc_xmlutil.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #ifndef XMLUTIL_H_
00026 #define XMLUTIL_H_
00027 #include <QString>
00028 #include <QXmlStreamWriter>
00029 #include <QXmlStreamReader>
00030 #include <QtDebug>
00031 
00032 namespace glcXmlUtil
00033 {
00035 inline bool goToElement(QXmlStreamReader* pReader, const QString& element);
00036 
00037 // Return the content of an element
00038 inline QString getContent(QXmlStreamReader* pReader, const QString& element);
00039 
00041 inline QString readAttribute(QXmlStreamReader* pReader, const QString& attribute);
00042 
00044 inline bool endElementNotReached(QXmlStreamReader* pReader, const QString& element);
00045 
00047 inline bool startElementNotReached(QXmlStreamReader* pReader, const QString& element);
00048 
00050 inline void goToEndElement(QXmlStreamReader* pReader, const QString& element);
00051 
00052 };
00053 
00054 
00055 bool glcXmlUtil::goToElement(QXmlStreamReader* pReader, const QString& element)
00056 {
00057         while(!pReader->atEnd() && !(pReader->isStartElement() && (pReader->name() == element)))
00058         {
00059                 pReader->readNext();
00060         }
00061         return !pReader->atEnd();
00062 }
00063 
00064 QString glcXmlUtil::getContent(QXmlStreamReader* pReader, const QString& element)
00065 {
00066         QString content;
00067         while(endElementNotReached(pReader, element))
00068         {
00069                 pReader->readNext();
00070                 if (pReader->isCharacters() && !pReader->text().isEmpty())
00071                 {
00072                         content+= pReader->text().toString();
00073                 }
00074         }
00075 
00076         return content.trimmed();
00077 }
00078 
00079 QString glcXmlUtil::readAttribute(QXmlStreamReader* pReader, const QString& attribute)
00080 {
00081         return pReader->attributes().value(attribute).toString();
00082 }
00083 
00084 bool glcXmlUtil::endElementNotReached(QXmlStreamReader* pReader, const QString& element)
00085 {
00086         return !pReader->atEnd() && !(pReader->isEndElement() && (pReader->name() == element));
00087 }
00088 
00089 bool glcXmlUtil::startElementNotReached(QXmlStreamReader* pReader, const QString& element)
00090 {
00091         return !pReader->atEnd() && !(pReader->isStartElement() && (pReader->name() == element));
00092 }
00093 
00094 void glcXmlUtil::goToEndElement(QXmlStreamReader* pReader, const QString& element)
00095 {
00096         while(endElementNotReached(pReader, element))
00097         {
00098                 pReader->readNext();
00099         }
00100 }
00101 
00102 #endif /* XMLUTIL_H_ */

SourceForge.net Logo

©2005-2011 Laurent Ribon