[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-edu-devel
Subject:    Re: [kde-edu-devel] XML and me.....
From:       Kevin Krammer <kevin.krammer () gmx ! at>
Date:       2002-05-29 15:29:50
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday, 29. May 2002 13:33, Scott Wheeler wrote:
> So the two remaining classes are XMLParser and Card.  XMLParser is a
> subclass of QXmlDefaultHandler and it's implementation came more or less
> out of the the Qt documentation for the XML module.

I found it easier to use the DOM approach unsign QDom classes.
But I have to admit that I didn't look at the XMLParser stuff very closely as 
I assumed it would be similar to SAX.

DOM, the document object model, give you less control over the parsing 
process, but you don't have to build your internal structure during parsing.

Example:
This is a keduca file (at least that's what an older KEduca produced :))

<?xml version = '1.0' encoding = 'iso8859-15' standalone = 'yes'?><!DOCTYPE 
educa ><Document>
 <Info>
  <title>MyTest</title>
  <category>Geographie</category>
  <type>1</type>
  <level>1</level>
  <language>at</language>
  <author>
   <name>Kevin Krammer</name>
   <email>kevin.krammer@gmx.at</email>
  </author>
 </Info>
 <Data>
  <question type="1" >
   <text>My first Question</text>
   <false>wrong answer 1</false>
   <true>true answer</true>
   <false>wrong answer 2</false>
  </question>
  <question type="1" >
   <text>My second question</text>
   <true>true answer</true>
   <false>wrong answer 1</false>
   <false>wrong anser 2</false>
  </question>
 </Data>
</Document>

Extracting the questions and answers with QDom works like this:

QFIle file("test.edu"); // asumming the file is named test.edu

QDomDocument domDoc;
domDoc.setContent(&file);

QDomElement rootElement = domDoc.documentElement();
// rootElement ist <Document>

// now I assume that <Data> is always the second child of Document
QDomElement dataElement = rootElement.firstChild().nextSibling();

// now we iterate over all the questions and extract their
// data in a seperate function

for (QDomNode domNode = dataElement.firstChild();
     !domNode.isNull(); domNode = domNode.nextSibling)
{
    // we assume all children of <Data> are <question>
    readQuestion(domNode.toElement());
}


// method to extract question data
void readQuestion(const QDomElement& questtionElement)
{
    // this would usually fill an internal structure
    // here I only do some debug output

    for (QDomNode domNode = questionElement.firstChild();
          !domNode.isNull(); domNode = domNode.nextSibling())
    {
        QDomElement domElem = domNode.toElement();
        if (domElem.nodeName() == "text")
        {
            kdDebug() << "Question: " << domElement.text() << endl;
        }
        else if (domElem.nodeName() == "true")
        {
            kdDebug() << "correct: " << domElement.text() << endl;
        }
        else if (domElem.nodeName() == "false")
        {
            kdDebug() << "wrong: " << domElement.text() << endl;
        }
    }
}


hth,
Kevin
- -- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kevin Krammer <kevin.krammer@gmx.at>
Developer at the Kmud Project http://www.kmud.de/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE89PPznKMhG6pzZJIRAs+bAJwM37YVDqztRIt3zhnf8m9Ml/u8uACeNprW
GrdZzrQaNWDlxuvyEECG5Pg=
=pZlZ
-----END PGP SIGNATURE-----
_______________________________________________
kde-edu-devel mailing list
kde-edu-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-edu-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic