XML Introduction by: Lester Melendez lmele002@cs.fiu.edu 1. What is XML? XML is a markup language for documents containing structured information. Structured information contains not only content but, also some indication of the role that content plays. A markup language is a mechanism to identify structures in a document. The XML specification defines a standard way to add markup to documents.[1] The entire XML specification can be referenced at [2]. XML documents are structured like trees. Each node is defined by a tag, similar to that found in HTML documents. The children of a node are defined by tags within the tags that define the node. The tree in figure 1.1 is depicted in pseudo-XML in figure 1.2. --------------- Figure 1.1 --------------- a / \ b c / \ / \ d e f g --------------- Figure 1.2 --------------- Though it is clear that XML is a very intuitive language, its nuances may at times escape even the most experienced of developers. It is important to carefully read all of the documentation associated with the XML standard. One such nuance is that XML nodes can contain data between begin and end tags. They can also contain attributes within begin tags. Figure 1.3 illustrates an XML node with both data and attributes. --------------- Figure 1.3 (data.xml) --------------- Steve's Car Mary's Car Figure 1.3 contains a lot of valuable information. Accessing and manipulating this information will be covered in subsequent sections. 2. How can we use XML? There are a variety of methods in which one can manipulate XML, among them are CSS, XSLT, and JavaScript. None of the available XML manipulation methods can be considered better than the others. It is up to the developer to determine the best approach for the task at hand. JavaScript has been known to have the ability to please the novice and professional developer alike. JavaScript affords developers a myriad of ways in which to manipulate XML. Through the use of JavaScript developers may do everything from run a query on the structured information within an XML file to displaying the results in standard HTML. The following section will describe the basics of XML manipulation using JavaScript. 3. Loading XML Into Memory This is the first step in using JavaScript to manipulate XML. Before one can begin accessing an XML file within JavaScript, the file need to be loaded into memory through the use of a browser's XML parser. All modern browsers have a build-in XML parser that can be used to read and manipulate XML. The parser reads XML into memory and converts it into an XML DOM object that can be accessed with JavaScript.[3] The code for parsing an XML document is illustrated in figure 3.1 --------------- Figure 3.1 --------------- One can clearly notice the conditional statements that allow for the use of many of today's browsers. The code in figure 3.1 loads data.xml into memory, storing it as xmlDoc and thus allowing for the use of XML DOM (XML Document Object Model)[4]. Knowledge of DOM (Document Object Model)[5] would be very useful in understanding XML DOM. 4. XML DOM The XML DOM defines a standard way for accessing and manipulating XML documents. The DOM views XML documents as a tree-structure. All elements can be accessed through the DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can be created. The elements, their text, and their attributes are all known as nodes.[6] Use of XML DOM requires that one load into memory the set of nodes one would like to manipulate. This can be done by loading the nodes into a variable by using a line similar to that in figure 4.1. Again, we are referring to data.xml for all of our examples. --------------- Figure 4.1 --------------- var x=xmlDoc.getElementsByTagName("employee"); This line gives us an array of all employee nodes. getElementsByTagName is a method that we are able to use courtesy of the XML DOM. There are many other methods available to us. For further detail see [4]. Visual inspection of data.xml shows us that each employee node has two attributes, age and name. Thanks to the XML DOM we are able to not only access these attributes but, perform a variety of operations on them. Let's say one wanted to find out if there was an employee by the name of mary. We would use standard conditional statements in conjunction with XML DOM to achieve our results. The example in figure 4.2 illustrates just that. --------------- Figure 4.2 (CAN BE PLACED INTO FIGURE 1.3 --------------- var x=xmlDoc.getElementsByTagName("employee"); for (i=0;i" + x[i].getAttribute("name") + "" + "" + " " +x[i].getAttribute("age") + ""); document.write("
"); } You can clearly note, that formatting your XML information is nothing more than manipulating it through the use of XML DOM and then placing HTML tags around it within the document.write method. 7. Further Reading [8]