How it works…

Similar to the previous example, we first initiate the file dialog and declare a QDomDocument object. Then, we create the root element by calling QDomDocument::createElement(). Any element created from QDomDocument will not automatically become its direct child unless we append the newly-created element as its child.

To create the grandchildren of QDomDocument, simply append the newly-created elements to the root element. By utilizing the append() function, we can easily arrange the XML data in a tree structure without wrapping our head around it. This, in my opinion, is the advantage of using QDomDocument instead of QXmlStreamReader.

We can then add attributes to an element by calling QDomElement::setAttribute(). We can also create a text node by calling QDomDocument::createTextNode() and appending it to any of the elements in the XML structure. After we are done structuring the XML data, we can output all the data in the form of text to the QTextStream class and allow it to save the data into a file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset