开发者

How to import XML data in Qt

I'm confused as to how to import data from a XML file. The XML file is structured as follows:

<Workflow>
     <ItemList1>
         <Item1>1</Item1>
         <otherItem>1</otherItem>
         <anotherItem>1</anotherItem>
          ........................
     </ItemList1>
     <TaskLists>
        <NumberOfTasks>2</NumberOfTasks>
        <Task_1>
           <description>"description"</description>
           <position>"x, y"</position>
           <name>"name"</name>
           <tagListNumberOfItems>2</tagListNumberOfItems>
           <tagList>
              <subTag>"text"</subTag>
              <other_subTag>"text"</other_subTag>
           </tagList>
        </Task_1>
        <Task_2>
           <description>"description"</description>
 开发者_如何转开发          <position>"x,y"</position>
           <name>"name"</name>
           <tagListNumberOfItems>4</tagListNumberOfItems>
           <tagList>
              <different_subTag>"text"</different_subTag>
              <other_different_subTag>"text"</other_different_subTag>
              <a_3rd_subTag>"text"</a_3rd_subTag>
              <a_4th_subTag>"text"</a_4th_subTag>
           </tagList>
        </Task_2>
     </TaskLists>
</Workflow>

How should I import that data? Thanks!


The easiest is to look at the Bookmark Example. It makes use of the QXmlStreamReader

From the doc:

   QXmlStreamReader xml;
   ...
   while (!xml.atEnd()) {
         xml.readNext();
         ... // do processing
   }
   if (xml.hasError()) {
         ... // do error handling
   }

From the example:

bool XbelReader::read(QIODevice *device)
 {
     xml.setDevice(device);

     if (xml.readNextStartElement()) {
         if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0")
             readXBEL();
         else
             xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
     }

     return !xml.error();
 }


void XbelReader::readXBEL()
 {
     Q_ASSERT(xml.isStartElement() && xml.name() == "xbel");

     while (xml.readNextStartElement()) {
         if (xml.name() == "folder")
             readFolder(0);
         else if (xml.name() == "bookmark")
             readBookmark(0);
         else if (xml.name() == "separator")
             readSeparator(0);
         else
             xml.skipCurrentElement();
     }
 }

You can use DOM, SAX or XmlStream. Take a look Here for a couple of examples.

Thus, you read the xml and then create / populate your objects/runtime depending on the what the XML file provided.


Take a look at QtXml module.

I think that this class will do what you need: http://doc.qt.io/qt-5/qdomdocument.html

It allows you to load an XML file as a tree that you can read or modify.


I know you are asking this question because you are not able to get a working sample code anywhere to read an XML file. The following code helps you to do just that.

QString fileName = "yourfile.xml";
QFile file(fileName);
if(file.exists()) {

    QDomDocument doc( "XMLFile" );
    if( !file.open( QIODevice::ReadOnly ) )
        return false;
    if( !doc.setContent( &file ) )
    {
        file.close();
        return false;
    }
    file.close();
    QDomElement root = doc.documentElement();
    QDomNode n = root.firstChild();
    while( !n.isNull() )
    {
        QDomElement e = n.toElement();
        if( !e.isNull() )
        {
            qDebug() << e.tagName(); //this gives you the name of the tag
            qDebug() << e.namedItem("ChildTag").toElement().text(); //this gives you the node value of a tag.

        }
        n = n.nextSibling();
    }
} else {
    return false;
}
return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜