How to use XML data in a Web Application
posted by 'Gregory Gates' on April 16, 2008 at 9:00 pm
XML is by far one of the most robust way of representing data in a textual format invented in the computer industry. XML can see its roots in HTML, but its various implementations has helped move HTML forward making more robust web applications and dynamic pages using AJAX. This article reviews how XML has been used for the creation of a more robust ICCA web site by being used in as a standalone data source and how XML has been integrated into the database schema. Along with the all the wonders of XML comes a few speed bumps with its integration into the PHP language, nothing serious, but it is important to include.
First, the quick housework, XML is the eXtensible Markup Language and AJAX is Asynchronous Javascript And XML. Lots of documentation about XML, AJAX and Javascript is around, almost too much, this is not a tutorial, it is how it has been integrated into one web application, the new ICCA web site.A database has traditionally been the home of all data used by web applications. By creating new tables, all data can be stored in separate columns making access easy and consistent. The old ICCA web site managed all its data in the Microsoft Access database which could easily have been mapped to the new MySQL database.
This seems like the obvious choice but there were some nagging questions: was all the data known or would the data change based on new needs of the system; was this the best way of storing the data now that XML was available; is there a better way?
A quick review of the types of data needed for storage started with the old ICCA web site. The Microsoft Access database had over twenty tables including storage for the board of directors, the subcommittee and committee members, the volunteer groups and volunteer members, the names of all the chapters and the chapters members, the entire membership and the member firms, the member signup transactions, the member renewals, the member purchases, the national conference registration, the webinar registration and more. Basically, there were lots of tables and it appeared that new tables were created when they were needed, it appeared very disorganized. Was this the best that could be done, should this database schema be mapped to the new MySQL database.
In a word, No!
In deciding the new database schema, the handling of XML database became the way of creating a simple ten table de-normalized schema that could handle most of the current needs of the new ICCA web application. The first decision is removing the non-critical data from the database including the list of board positions, the list of chapters, the ICCA products sold, the list of webinars and the list of conferences. The next decision was storing XML data in the database in MySQL medium text fields where the contents where dynamic but for a single purpose, like the chapter board of directors and chapter meeting information, the conference options and the electronic commerce shopping cart contents.
How this works is simple. The web application uses a set of PHP based classes maintaining the data, the methods read the data from either XML, the database columns directly, or the XML data from a database column. These data access classes return the contents in simple PHP arrays. The higher level PHP classes need the data but have no knowledge of where the actual data is stored. When new data is needed for storage, new XML data is created without the need of creating new database columns. It all works smoothly.
Finally, the speed bumps come in the PHP arrays. XML data can have information stored as attributes or as elements, it is the interpretation of the hierarchy of elements that cause the problem. The PHP arrays are typically flat in nature while the XML data can be represented as a tree of data, not flat. How to flatten XML data into an array without loosing context and how not to have element names step on attribute names when building the PHP arrays? If the XML data could be used directly, then the hierarchy could be maintained, but that is not how databases work. Database data is more like the XML attribute representation where there is not hierarchy. Database data can represented better in the PHP array, while XML element data is represented better as hierarchical tree.
The new ICCA web site has been designed to limit the use of hierarchical XML data and making the best use of PHP arrays. This may not be the best integration of XML data but it works... so far. The lesson learned here is very basic but permeates throughout the entire web application and becomes a design requirement for the future. Although this may never really be an issue in the future, it is good to note for the current site implementation and for those interested.
Comments No comments for this entry... Add