There's moreā€¦

Besides the web browser, many commercial game engines and interactive applications use the XML format to store information for in-game scenes, meshes, and other forms of assets used in their products. This is because the XML format provides many benefits over other file formats, such as a compact file size, high flexibility and extendibility, easy file recovery, and a relational tree structure that allows it to be used for highly efficient and performance-critical applications, such as search engines, intelligent data mining servers, and scientific simulations.

Let's learn a little bit about the format of an XML file. We will use scene.xml, which we used in the previous example in the Processing XML data using the stream reader recipe, and looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<scene>
<object tag="building">
<name>Library</name>
<position>120.0,0.0,50.68</position>
<rotation>0.0,0.0,0.0</rotation>
<scale>1.0,1.0,1.0</scale>
</object>

There can be more than one object tag:

    <object tag="building">
<name>Town Hall</name>
<position>80.2,0.0,20.5</position>
<rotation>0.0,0.0,0.0</rotation>
<scale>1.0,1.0,1.0</scale>
</object>
<object tag="prop">
<name>Tree</name>
<position>10.46,-0.2,80.2</position>
<rotation>0.0,0.0,0.0</rotation>
<scale>1.0,1.0,1.0</scale>
</object>
</scene>

In XML, a tag is a line of markup text that starts with a < symbol and ends with a > symbol; <scene> is a tag called scene, <object> is a tag called object, and so on. Tags come in three flavors:

  • The start tag, for example <scene>
  • The end tag, for example </scene>
  • An empty-element tag, for example <scene />

Whenever you write a start tag, it must end with an end tag, otherwise your XML data will be invalid. An empty-element tag, however, is a standalone tag and does not need an end tag behind it.

At the top of the scene.xml file, you will see a tag called xml, which stores the version of the XML format and the encoding type, which in this case is XML version 1.0 and UTF-8 (8-bit Unicode) encoding. This line is called the XML declaration and it must exist in one of your XML files to validate its format.

After that, you will see tags that have attributes stored in them, for example <object tag="building">. This means that the object tag contains an attribute called tag, which contains a value, building. You can put as many attributes as you like in a tag, for example <object tag="building" color="red" name="LA Community Hospital" coordinate="34.0191757,-118.2567239">. Each of these attributes stores distinct data that can be retrieved easily using Qt.

You can also store data between the start tag and the end tag; for example, <name>Town Hall</name>. This method, however, is not relevant to the empty-element tag, since it is a standalone tag and isn't followed by an end tag. Therefore, you can only store attributes in an empty-element tag.

To learn more about the XML format, visit http://www.w3schools.com/xml.
..................Content has been hidden....................

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