c++qt5.5qxmlstreamreader

Qt reading XML structure


I am using Qt5.5, I want to read an XML file into memory maintaining the structure and attributes of the nodes and relationships.

So far I've been using QXmlStreamReader, however this doesn't really do much and I cannot see any obvious way to build the relationship between nodes.

What I ultimately want is to read the XML into a linked list where the root node is the same as the root xml node and other nodes are children of this.

After a little searching around, I found QDomDocument and QDomElement, however in order to use these the '.pro' file must be modified and xml should be appended to the QT line.


Solution

  • What you want is a DOM Parser. It provides a tree-like model (Document Object Model) of the XML data, that you can traverse, modify, and store to a file.

    QXmlStreamReader provides a very simple interface similar to a SAX parser. If you simply need to extract data from XML fromatted text, it might be sufficient. (For writing simple XML, there is also a QXmlStreamWriter). These two classes are included in the Qt Core module

    If you need a more powerful XML Parser, the Qt XML Module offers a real SAX Parser (QXmlSimpleReader) and a DOM Parser (QDomDocument). You can activate in qmake projects by adding qt += xml in the pro-file.