user2216648
user2216648

Reputation: 41

Parsing XML File with QXmlStreamReader C /Qt

I try to parse an XML File with my programme cpp, and to stock it on a QList, but i dont know why when i execute the code the application return nothing ( and sometimes it close alone )

this is my function :

void mission::loadMission(QList < waypoint* > wpList , QString filename){

     QXmlStreamReader xmlReader;
      int i=0 ;
      QFile file(filename);

      if(file.open(QIODevice::ReadOnly)) {
      xmlReader.setDevice(&file);;
      xmlReader.readNext();
      xmlReader.readNext();
      bool ok = false ;

      //Reading from the file
      while (!xmlReader.isEndDocument())
      {
          if (xmlReader.isStartElement())
          {
              QString name = xmlReader.name().toString();
              if (name == "Number" )
              {
                  wpList[i]->setNum(xmlReader.readElementText().toInt(&ok,10));
              }

              else  if (name == "Longitude" )
              {
                  wpList[i]->setLong(xmlReader.readElementText().toDouble(&ok));
              }

              else  if (name == "Latitude" )
              {
                  wpList[i]->setLat(xmlReader.readElementText().toDouble(&ok));
              }

              else  if (name == "Altitude" )
              {
                  wpList[i]->setAlt(xmlReader.readElementText().toDouble(&ok));
              }

              else  if (name == "Heading" )
              {
                  wpList[i]->setHdg(xmlReader.readElementText().toDouble(&ok));
              }

              else  if (name == "Time" )
              {
                  wpList[i]->setTime(xmlReader.readElementText().toInt(&ok,10));
              }

              else  if (name == "Type" )
              {
                  wpList[i]->setType(xmlReader.readElementText().toInt(&ok,10));
              }

          }
          else if (xmlReader.isEndElement())
          {
              xmlReader.readNext();
              i++ ;
          }
      }
      if (xmlReader.hasError())
      {
          cout << "XML error: " << xmlReader.errorString().data() << std::endl;
      }

  }



}

and this an exemple for my XML file :

<?xml version="1.0" encoding="UTF-8"?>
<Mission>
    <Number>2013_7_11_16_28</Number>
    <Waypoint>
        <Number>0</Number>
        <Longitude>1.26946</Longitude>
        <Latitude>43.5147</Latitude>
        <Altitude>100</Altitude>
        <Heading>90</Heading>
        <Time>4400</Time>
        <Type>1</Type>
    </Waypoint>
    <Waypoint>
        <Number>1</Number>
        <Longitude>1.56958</Longitude>
        <Latitude>43.4721</Latitude>
        <Altitude>100</Altitude>
        <Heading>90</Heading>
        <Time>4400</Time>
        <Type>1</Type>
    </Waypoint>
    <Waypoint>
        <Number>2</Number>
        <Longitude>1.64424</Longitude>
        <Latitude>43.655</Latitude>
        <Altitude>100</Altitude>
        <Heading>90</Heading>
        <Time>4400</Time>
        <Type>1</Type>
    </Waypoint>
  </Mission>

Upvotes: 1

Views: 3791

Answers (1)

Son-Huy Pham
Son-Huy Pham

Reputation: 1899

I would recommend you keep working/learning with regular C++ & STL and then move on to Qt when you're more proficient. Some of the things I've noticed is regarding basic C++ programming and has nothing to do with Qt really.

For starters, need to pass by reference instead of by value.

 void mission::loadMission(QList<waypoint*> &wpList , QString filename){  
     // ...
 }

There's probably little to gain by storing a QList of pointers. I would also just have QList < waypoint> &. Then use the QList::push_back() method and populate the list instead of trying to guess where to index the list.

          void mission::loadMission(QList<waypoint> &wpList , QString filename){  
              // ...
              QStringRef name = xmlReader.name();
              if (name == "Waypoint" )
              {
                  waypoint wp;
                  wpList.push_back(wp);
              }
              if (name == "Number" )
              {
                  wpList.back().setNum(xmlReader.readElementText().toInt(&ok,10));
              }

It is also unnecessary to make a temporary copy of QString from QStringRef

          QStringRef name = xmlReader.name();
          if (name == "Number" )
          { // ...
          }

Upvotes: 1

Related Questions