Efficient XML--XDS

Source: Internet
Author: User

Efficient XML--XDS by mikefeng recently saw an introduction to xds in game programming Gems 4, unlocking my confusions about XML inefficiency. It may not be seen in small XML applications, but in applications with large data volumes, the XML speed cannot even be compared with that of common. ini. First, let's take a look at what xds is. Xds technology consists of two file formats: DSD and xds. The former is similar to XSD, while the latter is similar to XML, except that both formats are binary. The binary format is used, and the xds performance is significantly higher than XML in terms of volume and speed. Currently, the free xds Library supports xdstoolkit, which can be downloaded to version 1.03. This is an open-source project. After decompression, we can see that it consists of two tools, one API package, and an example. The two tools are named xdsconvert and xdsmakeschema respectively, which are used to convert XML and xds and generate DSD files. In a C/C ++ project, we often need to use struct to define a series of data structures. Xdsmakeschema can generate DSD and the corresponding C header file through the input data structure definition file. h. In the early stage of a project, you may need to use the XML editor to compile the XML data required by the project, and then parse the data through xdsliteapi in the program. This API has two pasers, one serving XML and the other serving xds. When your project can automatically generate xml, you can switch from XML to xds. The essence of game programming is explained in detail. Here we will talk about what needs to be paid attention. To use APIs to parse xds, perform the following steps: ① define the C data type defined by struct ② define the xds data type. In DSD, you can also define ③ callback function writing in the program. The xds_processnode function uses the powerup function included in the Toolkit as an example. struct looks like this: struct powerup_t {char szname [10]; // display name char szimage [16]; // image file name // health increase/decrease (-128 to 127) signed Char ihealth; // temporary abilities/penalties // (value is duration in seconds) unsigned char iinvulnerability; unsigned char IFAS Tmove; unsigned char ihighjump; unsigned char istunplayer; // Extra Life (count) unsigned char ilifeup;}; // global power-up definition cacheextern struct powerup_t * g_powerups; you can use xdsmakeschema to generate a DSD. The xxxx_dsd.h generated at the same time is just to avoid reading the DSD file into the memory. view its content and you will see that it defines a DSD array: // xds DSD literal -- use this in callto xdsinit () // # ifdef define_dsd const unsigned char xdsdsd_powerups [216] = {0x58, 0x44, 0x53, 0x21, 0x30, 0x33 ,...}; # Else extern const unsigned char xdsdsd_powerups [216]; # endif // xds DSD IDs -- use these in implementation of xds_processnode () // # define xds_powerups_powerup 0x0100 // record # define limit 0x0101 // type # define xds_powerups _ xdstype1 0x0102 // type # define xds_powerups_g_powerups 0x0103 // element also defines some constants, these constants are used in parsing xds. In addition to the definition of the xds format in DSD, we can also. the definition in the program is displayed in CPP: # ifdef xds_support_deftypevoid regdsd (struct xdshandle * hxds) {// register my types (test only) xdsdefrecord (hxds, "powerup", 2 ); unsigned short istructtype = struct (hxds, "powerup_t"); xdsdefstructfield (hxds, istructtype, "szname", fingerprint, 10); xdsdefstructfield (hxds, istructtype, "szimage", expires, 16); xdsdefstructfield (hxds, is Tructtype, "ihealth", xds_type_char, 0); xdsdefstructfield (hxds, istructtype, "iinvulnerability", priority, 0); xdsdefstructfield (hxds, istructtype, "ifastmove", priority, 0); xdsdefstructfield (hxds, istructtype, "ihighjump", blocks, 0); xdsdefstructfield (hxds, istructtype, "istunplayer", blocks, 0); xdsdefstructfield (hxds, istructtype, "ilifeup", xds_type_byte, 0); xdsdefst Ructdone (hxds, istructtype); unsigned short iarraytype = struct (hxds, "_ xdstype1", istructtype, 0, 2); xdsdefelement (hxds, "g_powerups", iarraytype, 0) ;}# endif Note: Cross-Use of DSD definitions and program definitions may easily cause an error, that is, conflicts between programs and DSD may occur, and data types may conflict, or Data Length conflicts, resulting in program crash. In the attached example, the program defines the data type as follows: # ifdef xds_support_deftypevoid regdsd (struct xdshandle * hxds) {// register my types (test only) xdsdefrecord (hxds, "powerup ", 4); unsigned short istructtype = xdsdefstructtype (hxds, "powerup_t"); xdsdefstructfield (hxds, istructtype, "szname", struct, 10); xdsdefstructfield (hxds, istructtype, "szimage", xds_type_char, 16); xdsdefstructfield (hxds, istructtype, "ihealth", xds_t Ype_char, 0); xdsdefstructfield (hxds, istructtype, "struct", struct, 0); xdsdefstructfield (hxds, istructtype, "ifastmove", struct, 0); xdsdefstructfield (hxds, istructtype, "ihighjump", xds_type_byte, 0); xdsdefstructfield (hxds, istructtype, "istunplayer", role, 0); xdsdefstructfield (hxds, istructtype, "ilifeup", role, 0); xdsdefstructdone (hxds, istructtype ); Unsigned short iarraytype = xdsdefarraytype (hxds, "_ xdstype1", istructtype, 0, 2); xdsdefelement (hxds, "g_powerups", iarraytype, 0 );} # If endif uses the-r powerup: 2 parameter when DSD is generated, and xdsdefrecord (hxds, "powerup", 4) is used here, a conflict will occur.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.