Parse a xml file using c++ & Qt

后端 未结 3 1872
夕颜
夕颜 2021-02-09 19:51

I try to parse a XML-file with the following structure:


  
     
        

        
相关标签:
3条回答
  • 2021-02-09 20:15

    For XML things, it was suggested to use QXmlStreamReader and QXmlStreamWriter from QtCore module, just because the QDom and QSax things have been not actively maintained for a while.

    http://doc.qt.digia.com/4.7/qxmlstreamreader.html

    http://doc.qt.digia.com/4.7/qxmlstreamwriter.html

    I will not copy&paste the example code from qt docs to here. Hope you could understand them well. And you also could check examples/xml directory in qt 4.x.

    0 讨论(0)
  • 2021-02-09 20:34

    Untested, but this is a way I already used Qt to scan in a very simply XML file. Maybe this can give you a hint how to use it here:

    QDomElement docElem;
    QDomDocument xmldoc;
    
    xmldoc.setContent(YOUR_XML_DATA);
    docElem=xmldoc.documentElement();
    
    if (docElem.nodeName().compare("T")==0)
    {
        QDomNode node=docElem.firstChild();
        while (!node.isNull())
        {
            quint32 number = node.toElement().attribute("t").toUInt(); //or whatever you want to find here..
            //do something
            node = node.nextSibling();
        }
    }
    
    0 讨论(0)
  • 2021-02-09 20:35

    you could use QXmlQuery. It act like XQuery (i guess the syntax is the same). And you could parse your xml file with the big advantage of XQuery's flexibility. You can start with a code like this:

    QByteArray myDocument;
    QBuffer buffer(&myDocument); // This is a QIODevice.
    buffer.open(QIODevice::ReadOnly);
    QXmlQuery query;
    query.bindVariable("myDocument", &buffer);
    query.setQuery("doc($myDocument)");
    

    setQuery method allow you to define your search pattern. It can be based on element id, attribute, and so on...as with XQuery. This is QXmlQuery doc page: link

    0 讨论(0)
提交回复
热议问题