Beispiel #1
0
bool CBXmlReader::readAll()
{
    if (!_streamReader)
        return false;

    QXmlStreamAttributes attrs;
    QString tag = "";
    QString text = "";

    while(!_streamReader->atEnd()) {
        switch (_streamReader->tokenType())
        {
        case QXmlStreamReader::NoToken:
        case QXmlStreamReader::StartDocument:
        case QXmlStreamReader::EndDocument:
            break;
        case QXmlStreamReader::StartElement:
            tag = _streamReader->name().toString();
            attrs = _streamReader->attributes();
            break;
        case QXmlStreamReader::Characters:
        case QXmlStreamReader::Comment:
            text = _streamReader->text().toString();
            break;
        case QXmlStreamReader::EndElement:
            if (_streamReader->name().toString().compare(tag) != 0)
            {
                _streamReader->readNext();
                continue;
            }

            if (_callback) {
                if (!_callback->onTagDetected(tag, text, attrs))
                    goto error;
            }
            tag = "";
            text = "";
            attrs.clear();
            break;
        case QXmlStreamReader::Invalid:
            goto error;
        default:
            break;
        }

        _streamReader->readNext();
    }

    return true;

error:
    if (_streamReader->hasError()) {
        qDebug()<<_streamReader->errorString();
    }
    return false;
}
Beispiel #2
0
void LapTimerThread::writeXml()
{
    QXmlStreamReader xmlReader;
    coordTmpFile.open(QIODevice::ReadOnly | QIODevice::Text);
    xmlReader.setDevice(&coordTmpFile);
    xmlReader.readNext();
    int lapNum=0;
    //Reading from the file
    xmlReader.readNext();
    while (!xmlReader.isEndDocument())
    {
        if (xmlReader.isStartElement())
        {
            QString name = xmlReader.name().toString();
            if (name == "lap")
            {
                lapNum++;
            }

        }
        else if (xmlReader.isEndElement())
        {

        }
        xmlReader.readNext();
    }
    coordTmpFile.close();
    for (int i=0;i<lapNum-1;i++)// loop to generate kml file for each lap
    {

    QFile xmlKmlFile;
    QXmlStreamWriter bufferKml;

    xmlKmlFile.setFileName("data/"+dirName+"/"+dirName+"-"+QString::number(i+1)+".kml");
    xmlKmlFile.open(QIODevice::WriteOnly | QIODevice::Text);
    bufferKml.setDevice(&xmlKmlFile);
    bufferKml.setAutoFormatting(true);
    bufferKml.writeStartDocument();

    bufferKml.writeStartElement("kml");
    QXmlStreamAttributes atrr;
    atrr.append("xmlns","http://www.opengis.net/kml/2.2");
    atrr.append("xmlns:gx","http://www.google.com/kml/ext/2.2");
    atrr.append("xmlns:atom","http://www.w3.org/2005/Atom");
    bufferKml.writeAttributes(atrr);
    bufferKml.writeStartElement("Document");
    bufferKml.writeTextElement("open","1");
    bufferKml.writeTextElement("visibility","1");
    bufferKml.writeStartElement("name");
    bufferKml.writeCDATA("GPS Lap Timer - KML Log - "+QDate::currentDate().toString("yyyyMMdd")+QTime::currentTime().toString("hhmmss"));
    bufferKml.writeEndElement();
    //style - track
    bufferKml.writeStartElement("Style");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","track"));
    bufferKml.writeStartElement("LineStyle");
    bufferKml.writeTextElement("color","7f0000ff");
    bufferKml.writeTextElement("width","4");
    bufferKml.writeEndElement();
    bufferKml.writeStartElement("IconStyle");
    bufferKml.writeTextElement("scale","1.3");
    bufferKml.writeStartElement("Icon");
    bufferKml.writeTextElement("href","http://earth.google.com/images/kml-icons/track-directional/track-0.png");
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    //style - start
    bufferKml.writeStartElement("Style");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","start"));
    bufferKml.writeStartElement("IconStyle");
    bufferKml.writeTextElement("scale","1.3");
    bufferKml.writeStartElement("Icon");
    bufferKml.writeTextElement("href","http://maps.google.com/mapfiles/kml/paddle/grn-circle.png");
    bufferKml.writeEndElement();
    bufferKml.writeEmptyElement("hotSpot");
    atrr.clear();
    atrr.append("x","32");
    atrr.append("y","1");
    atrr.append("xunits","pixels");
    atrr.append("yunits","pixels");
    bufferKml.writeAttributes(atrr);
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    // style - end
    bufferKml.writeStartElement("Style");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","end"));
    bufferKml.writeStartElement("IconStyle");
    bufferKml.writeTextElement("scale","1.3");
    bufferKml.writeStartElement("Icon");
    bufferKml.writeTextElement("href","http://maps.google.com/mapfiles/kml/paddle/red-circle.png");
    bufferKml.writeEndElement();
    bufferKml.writeEmptyElement("hotSpot");
    bufferKml.writeAttributes(atrr);
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    // style - statistics
    bufferKml.writeStartElement("Style");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","statistics"));
    bufferKml.writeStartElement("IconStyle");
    bufferKml.writeTextElement("scale","1.3");
    bufferKml.writeStartElement("Icon");
    bufferKml.writeTextElement("href","http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png");
    bufferKml.writeEndElement();
    bufferKml.writeEmptyElement("hotSpot");
    atrr.clear();
    atrr.append("x","20");
    atrr.append("y","2");
    atrr.append("xunits","pixels");
    atrr.append("yunits","pixels");
    bufferKml.writeAttributes(atrr);
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    // style - waypoint
    bufferKml.writeStartElement("Style");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","waypoint"));
    bufferKml.writeStartElement("IconStyle");
    bufferKml.writeTextElement("scale","1.3");
    bufferKml.writeStartElement("Icon");
    bufferKml.writeTextElement("href","http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png");
    bufferKml.writeEndElement();
    bufferKml.writeEmptyElement("hotSpot");
    bufferKml.writeAttributes(atrr);
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();

    QStringList xmlTmpTextBuffer;
    int lapTmpNum=0;
    bool lapNumFlag=false;

    bufferKml.writeStartElement("Folder");
    bufferKml.writeStartElement("name");
    bufferKml.writeCDATA("Lap #"+QString::number(i+1));
    bufferKml.writeEndElement();
    bufferKml.writeTextElement("open","1");

    //waypoints - load
    xmlReader.clear();
    wayptTmpFile.open(QIODevice::ReadOnly | QIODevice::Text);
    xmlReader.setDevice(&wayptTmpFile);
    xmlReader.readNext();
    xmlTmpTextBuffer.clear();
    xmlTextBuffer.clear();
    lapTmpNum=0;
    lapNumFlag=false;
    QStringList bestBuffer,devBuffer,timeBuffer;
    while (!xmlReader.isEndDocument())
    {
        if (xmlReader.isStartElement())
        {
            QString name = xmlReader.name().toString();

            if (name == "when" && lapNumFlag)//read time of each element
            {
               xmlTextBuffer.append(xmlReader.readElementText());
            }
            else if (name == "waypt" && lapNumFlag)
            {
                xmlTmpTextBuffer.append(xmlReader.readElementText());
            }
            else if (name == "best" && lapNumFlag)
            {
                bestBuffer.append(xmlReader.readElementText());
            }
            else if (name == "time" && lapNumFlag)
            {
                timeBuffer.append(xmlReader.readElementText());
            }
            else if (name == "dev" && lapNumFlag)
            {
                devBuffer.append(xmlReader.readElementText());
            }
            else if (name == "lap")
            {
                lapNumFlag=(lapTmpNum==i)?true:false;
                lapTmpNum++;
            }
        }
        else if (xmlReader.isEndElement())
        {

        }
        xmlReader.readNext();
    }
    wayptTmpFile.close();
    //waypoint - write
    for (int j=0;j<xmlTextBuffer.length();j++)
    {
        bufferKml.writeStartElement("Placemark");
        bufferKml.writeStartElement("name");
        if ((j+1)==xmlTextBuffer.length())
        bufferKml.writeCDATA("Finish");
        else
        bufferKml.writeCDATA("Waypoint"+QString::number(j+1));
        bufferKml.writeEndElement();
        bufferKml.writeStartElement("description");
        bufferKml.writeCDATA("Time: "+timeBuffer.at(j)+"; To best: "+devBuffer.at(j)+"; Best: "+bestBuffer.at(j));
        bufferKml.writeEndElement();
        bufferKml.writeStartElement("TimeStamp");
        bufferKml.writeTextElement("when",xmlTextBuffer.at(j));
        bufferKml.writeEndElement();
        if (j==xmlTextBuffer.length()-1)
        bufferKml.writeTextElement("styleUrl","#end");
        else
        bufferKml.writeTextElement("styleUrl","#waypoint");
        bufferKml.writeStartElement("Point");
        bufferKml.writeTextElement("coordinates",xmlTmpTextBuffer.at(j));
        bufferKml.writeEndElement();
        bufferKml.writeEndElement();
    }

    //track - load
    xmlReader.clear();
    coordTmpFile.open(QIODevice::ReadOnly | QIODevice::Text);
    xmlReader.setDevice(&coordTmpFile);
    xmlReader.readNext();
    xmlTmpTextBuffer.clear();
    xmlTextBuffer.clear();
    lapTmpNum=0;
    lapNumFlag=false;
    while (!xmlReader.isEndDocument())
    {
        if (xmlReader.isStartElement())
        {
            QString name = xmlReader.name().toString();

            if (name == "when" && lapNumFlag)//read time of each element
            {
               xmlTextBuffer.append(xmlReader.readElementText());
            }
            else if (name == "coord" && lapNumFlag)
            {
                xmlTmpTextBuffer.append(xmlReader.readElementText());
            }
            else if (name == "lap")
            {
                lapNumFlag=(lapTmpNum==i)?true:false;
                                lapTmpNum++;
            }
        }
        else if (xmlReader.isEndElement())
        {

        }
        xmlReader.readNext();
    }
    coordTmpFile.close();
    //track - write
    bufferKml.writeStartElement("Placemark");
    bufferKml.writeAttribute(QXmlStreamAttribute("id","tour"));
    bufferKml.writeStartElement("name");
    bufferKml.writeCDATA("track");
    bufferKml.writeEndElement();
    bufferKml.writeStartElement("description");
    bufferKml.writeCDATA("");
    bufferKml.writeEndElement();
    bufferKml.writeTextElement("styleUrl","#track");
    bufferKml.writeStartElement("gx:MultiTrack");
    bufferKml.writeTextElement("altitudeMode","absolute");
    bufferKml.writeTextElement("gx:interpolate","1");
    bufferKml.writeStartElement("gx:Track");
    for (int j=0;j<xmlTextBuffer.length();j++)
    {
        bufferKml.writeTextElement("when",xmlTextBuffer.at(j));
        bufferKml.writeTextElement("gx:coord",xmlTmpTextBuffer.at(j));
    }
    //data - alt - write
    bufferKml.writeStartElement("ExtendedData");
    bufferKml.writeEmptyElement("SchemaData");
    bufferKml.writeAttribute(QXmlStreamAttribute("schemaUrl","#schema"));
    bufferKml.writeStartElement("gx:SimpleArrayData");
    bufferKml.writeAttribute(QXmlStreamAttribute("name","elevation"));
    for (int j=0;j<xmlTextBuffer.length();j++)
    {
        bufferKml.writeTextElement("gx:value",QString(xmlTmpTextBuffer.at(j)).section(" ",2,2));
    }
    bufferKml.writeEndElement();
    //data - speed - load
    xmlReader.clear();
    speedTmpFile.open(QIODevice::ReadOnly | QIODevice::Text);
    xmlReader.setDevice(&speedTmpFile);
    xmlReader.readNext();
    xmlTmpTextBuffer.clear();
    xmlTextBuffer.clear();
    lapTmpNum=0;
    lapNumFlag=false;
    while (!xmlReader.isEndDocument())
    {
        if (xmlReader.isStartElement())
        {
            QString name = xmlReader.name().toString();

            if (name == "speed" && lapNumFlag)//read time of each element
            {
               xmlTextBuffer.append(xmlReader.readElementText());
            }
            else if (name == "lap")
            {
                lapNumFlag=(lapTmpNum==i)?true:false;
                lapTmpNum++;
            }
        }
        else if (xmlReader.isEndElement())
        {

        }
        xmlReader.readNext();
    }
    speedTmpFile.close();
    //data - speed - write
    bufferKml.writeStartElement("gx:SimpleArrayData");
    bufferKml.writeAttribute(QXmlStreamAttribute("name","speed"));
    for (int j=0;j<xmlTextBuffer.length();j++)
    {
        bufferKml.writeTextElement("gx:value",xmlTextBuffer.at(j));
    }
    bufferKml.writeEndElement();

    // close all opened element
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();
    bufferKml.writeEndElement();

    bufferKml.writeEndDocument();
    xmlKmlFile.close();
  }
}