lib_LTLIBRARIES = libscience.la
+bin_PROGRAMS = libscience
+
libscience_la_SOURCES = \
element.cpp \
spectrum.cpp \
isotope.cpp \
spectrumparser.cpp \
elementparser.cpp \
- tempunit.cpp
+ tempunit.cpp \
+ main.cpp
libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0
libscience_la_LIBADD = $(LIB_KDEUI)
return e;
}
+
+
+
+
+
+////////////
+//
+
+ElementSaxParser::ElementSaxParser()
+: QXmlDefaultHandler(), currentElement_(0), inElement_(false), inName_(false)
+{
+}
+
+bool ElementSaxParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs)
+{
+ if (localName == "elementType") {
+ currentElement_ = new Element();
+ inElement_ = true;
+ } else if (inElement_ && localName == "scalar") {
+ for (int i = 0; i < attrs.length(); ++i) {
+ if (attrs.value(i) == "bo:name")
+ inName_ = true;
+ }
+ }
+ return true;
+}
+
+bool ElementSaxParser::endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&)
+{
+ if (localName == "elementType") {
+ inElement_ = false;
+ elements_.append(currentElement_);
+ currentElement_ = 0;
+ }
+ return true;
+}
+
+bool ElementSaxParser::characters(const QString &ch)
+{
+ if (inName_) {
+ kdDebug() << "nimi: " << ch << endl;
+ currentElement_->setName(ch);
+ inName_ = false;
+ }
+ return true;
+}
+
+QList<Element*> ElementSaxParser::getElements()
+{
+ return elements_;
+}
+#ifndef ELEMENTPARSER_H
+#define ELEMENTPARSER_H
/***************************************************************************
copyright : (C) 2005 by Carsten Niehaus
email : cniehaus@kde.org
#include <QStringList>
#include <qdom.h>
+#include <qxml.h>
+
class Element;
/**
};
+
+class ElementSaxParser : public QXmlDefaultHandler
+{
+ public:
+ ElementSaxParser();
+ bool startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs);
+ bool endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&);
+ bool characters(const QString &ch);
+ QList<Element*> getElements();
+
+ private:
+ Element *currentElement_;
+ QList<Element*> elements_;
+ bool inElement_;
+ bool inName_;
+
+};
+#endif // ELEMENTPARSER_H
--- /dev/null
+INCLUDES = -I$(top_srcdir)/libkdeedu/libscience $(all_includes)
+
+AM_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) $(KDE_RPATH)
+
+check_PROGRAMS = xmlreadingtest
+
+xmlreadingtest_SOURCES = xmlreadingtest.cpp
+xmlreadingtest_LDFLAGS = $(all_libraries)
+xmlreadingtest_LDADD = ../libscience.la
+
+METASOURCES = AUTO
--- /dev/null
+/* Sample parsing with QT's SAX2 by Riku Leino <tsoots@gmail.com> */
+
+#include "../elementparser.h"
+#include "../element.h"
+#include <kdebug.h>
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+ if (argc < 2 || argc > 2) {
+ std::cout << "Usage: elements <XML_FILE>\n";
+ return 1;
+ }
+
+ ElementSaxParser * parser = new ElementSaxParser();
+ QFile xmlFile(argv[1]);
+ QXmlInputSource source(xmlFile);
+ QXmlSimpleReader reader;
+ reader.setContentHandler(parser);
+ reader.parse(source);
+
+ QList<Element*> v = parser->getElements();
+
+ foreach( Element* e, v ){
+ kdDebug() << "Elementname: " << e->elementName() << ", mass: " << e->mass() << endl;
+ }
+
+ return 0;
+}