使用QXmlStreamReader C / Qt解析XML文件

时间:2013-07-12 14:41:27

标签: c++ xml qt parsing qxmlstreamreader

我尝试使用我的程序cpp解析XML文件,并将其存储在QList上,但我不知道为什么当我执行代码时应用程序不返回任何内容(有时单独关闭)

这是我的功能:

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;
      }

  }



}

这是我的XML文件的例子:

<?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>

1 个答案:

答案 0 :(得分:1)

我建议你继续使用常规C ++&amp; STL,然后当你更精通时继续前进到Qt。我注意到的一些事情是关于基本的C ++编程,并且与Qt无关。

对于初学者,需要通过引用而不是值传递。

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

通过存储指针的QList可能几乎没有什么好处。我也只有QList&lt;航点&GT; &放;.然后使用QList :: push_back()方法并填充列表,而不是试图猜测列表的索引位置。

          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));
              }

也无需从QStringRef

制作QString的临时副本
          QStringRef name = xmlReader.name();
          if (name == "Number" )
          { // ...
          }