XML stands for Extensible Markup Language and is a markup language used to describe data. It offers a standardized way to represent textual data. Often the XML data is also referred to as an XML document. The XML data doesnt perform anything on its own; to process that data, you need to use a piece of software called a parser . Unlike Hypertext Markup Language (HTML) , which focuses on how to present data, XML focuses on how to represent data. XML consists of user-defined tags, which means you are free to define and use your own tags in an XML document.
XML was approved as a recommendation by the World Wide Web Consortium (W3C) in February 1998. Naturally, this very fact contributed a lot to such a wide acceptance and support for XML in the software industry.
Now that you have brief idea about XML, lets see a simple XML document, as illustrated in Listing .
Many rules govern the creation of such XML documents. But we will save them for later discussion.
Benefits of XML
Why did XML become so popular? Well, this question has many answers, and I will present some of the important ones in this section.
XML Is an Industry Standard
As you learned previously, XML is a W3C recommendation. This means it is an industry standard governed by a vendor-independent body. History shows that vendor-specific proprietary standards dont get massive acceptance in the software industry. This non-acceptance affects overall cross-platform data sharing and integration. Being an industry standard has helped XML gain huge acceptance.
XML Is Self-Describing
XML documents are self-describing. Because of markup tags, they are more readable than, say, comma-separated values (CSV) files .
XML Is Extensible
Markup languages such as HTML have a fixed set of tags and attributesyou cannot add your own tags. XML, on the other hand, allows you to define your own markup tags.
XML Can Be Processed Easily
Traditionally, the CSV format was a common way to represent and transport data. However, to process such data, you need to know the exact location of the commas ( , ) or any other delimiter used. This makes reading and writing the document difficult. The problem becomes severe when you are dealing with a number of altogether different and unknown CSV files.
As I said earlier, XML documents can be processed by a piece of software called a parser. Because XML documents use markup tags, a parser can read them easily. Parsers are discussed in more detail later in this chapter.
XML Can Be Used to Easily Exchange Data
Integrating cross-platform and cross-vendor applications is always difficult and challenging. Exchanging data in heterogeneous systems is a key problem in such applications. Using XML as a data-exchange format makes your life easy. XML is an industry standard, so it has massive support, and almost all vendors support it in one way or another.
XML Can Be Used to Easily Share Data
The fact that XML is nothing but textual data ensures that it can be shared among heterogeneous systems. For example, how can the data generated by a Windows Forms application running on a Windows machine be accessible in a Java application running on a Unix box? XML is the answer.
XML Can Be Used to Create Specialized Vocabularies
As you already know, XML is an extensible standard. By using XML as a base, you can create your own vocabularies. Wireless Application Protocol (WAP) , Wireless Markup Language (WML) , and Simple Object Access Protocol (SOAP) are some examples of specialized XML vocabularies.
XML-Driven Applications
Now that you know the features and benefits of XML, lets see what all these benefits mean to modern software systems.
Figure shows a traditional web-based application . The application consists of ASP.NET pages hosted on a web server. The client, in the form of a web browser, requests various web pages. On receiving the requests, the web server processes them and sends the response in the form of HTML content. This architecture sounds good at first glance, but suffers from several shortcomings:
It considers only web browsers as clients.
The response from the web server is always in HTML. That means a desktop-based application may not render this response at all.
The data and presentation logic are tightly coupled. If we want to change the presentation of the same data, we need to make considerable changes.
Tomorrow, if some other application wants to consume the same data, it cannot be shared easily.
Figure 1-1.
Classic architecture for developing applications
Now, lets see how XML can come to the rescue in such situations.
Note
For the sake of easy understanding the example discussed here is deliberately kept very simple and straightforward. In a more realistic case there could be one or more services involved that return the XML data. The applications then consume those services as and when required. You will learn how XML and services are related in later chapters.
Have a look at Figure , where there are multiple types of clients. One is a web browser, and the other is a desktop application. Both send requests to the server along with XML data (if any). The server processes the requests and sends back the data in XML format. The web browser applies a style sheet (discussed later) to the XML data to transform it into HTML markup. The desktop application, on the other hand, parses the data by using an XML parser (discussed later) and displays it in a grid. Much more flexible than the previous architecture, isnt it? The advantages of the new architecture are as follows:
The application has multiple types of clients. It is not tied only to web browsers.
There is loose coupling between the client and the processing logic.
New types of clients can be added at any time without changing the processing logic on the server.