How it works…

Compared to QXmlStreamReader, the QDomDocument class is less straightforward when it comes to loading or saving XML data. However, QDomDocument does it in a strict way by making sure each element is linked to its respective parent element recursively, like in a tree structure. Unlike QXmlStreamReader, QDomDocument allows us to save data to an element created earlier, in a later time frame.

Since QDomDocument is not part of the Qt core library, we must add the XML module to our project manually. Otherwise, we will not be able to access QDomDocument and other classes related to it.

First, we load the XML file and extract its content to the QDomDocument class. Then, we get its document element, which acts as the root document, and obtain its direct children. We then convert each of the child nodes to QDomElement and obtain their tag names. By checking tag names, we are able to determine the type of data we're expecting from each element.

Since this is the first layer of elements with the tag name object, we don't expect any data from them; we repeat step 3 again, but this time we're going to do it on the element with the tag name object and obtain all its direct children, which means the grandchildren of the document element.

Again, by checking the tag name, we're able to know what data we can expect from its children elements. If the tag name matches the ones we're expecting (in this case, name, position, rotation, and scale), we can obtain its data by calling QDomElement::text().

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

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