GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_xmlutil.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #ifndef XMLUTIL_H_
26 #define XMLUTIL_H_
27 #include <QString>
28 #include <QXmlStreamWriter>
29 #include <QXmlStreamReader>
30 #include <QtDebug>
31 
32 namespace glcXmlUtil
33 {
35 inline bool goToElement(QXmlStreamReader* pReader, const QString& element);
36 
37 // Return the content of an element
38 inline QString getContent(QXmlStreamReader* pReader, const QString& element);
39 
41 inline QString readAttribute(QXmlStreamReader* pReader, const QString& attribute);
42 
44 inline bool endElementNotReached(QXmlStreamReader* pReader, const QString& element);
45 
47 inline bool startElementNotReached(QXmlStreamReader* pReader, const QString& element);
48 
50 inline void goToEndElement(QXmlStreamReader* pReader, const QString& element);
51 
52 };
53 
54 
55 bool glcXmlUtil::goToElement(QXmlStreamReader* pReader, const QString& element)
56 {
57  while(!pReader->atEnd() && !(pReader->isStartElement() && (pReader->name() == element)))
58  {
59  pReader->readNext();
60  }
61  return !pReader->atEnd();
62 }
63 
64 QString glcXmlUtil::getContent(QXmlStreamReader* pReader, const QString& element)
65 {
66  QString content;
67  while(endElementNotReached(pReader, element))
68  {
69  pReader->readNext();
70  if (pReader->isCharacters() && !pReader->text().isEmpty())
71  {
72  content+= pReader->text().toString();
73  }
74  }
75 
76  return content.trimmed();
77 }
78 
79 QString glcXmlUtil::readAttribute(QXmlStreamReader* pReader, const QString& attribute)
80 {
81  return pReader->attributes().value(attribute).toString();
82 }
83 
84 bool glcXmlUtil::endElementNotReached(QXmlStreamReader* pReader, const QString& element)
85 {
86  return !pReader->atEnd() && !(pReader->isEndElement() && (pReader->name() == element));
87 }
88 
89 bool glcXmlUtil::startElementNotReached(QXmlStreamReader* pReader, const QString& element)
90 {
91  return !pReader->atEnd() && !(pReader->isStartElement() && (pReader->name() == element));
92 }
93 
94 void glcXmlUtil::goToEndElement(QXmlStreamReader* pReader, const QString& element)
95 {
96  while(endElementNotReached(pReader, element))
97  {
98  pReader->readNext();
99  }
100 }
101 
102 #endif /* XMLUTIL_H_ */

©2005-2013 Laurent Ribon