Search This Blog

Monday, August 23, 2010

What is XML?

XML is a specification for creating your own markup languages. A document created according to the rules of the XML specification looks similar to HTML in that it contains elements and attributes coded as tags. Nevertheless, browsers do not attempt to directly format and display XML documents unless formatting information is provided in some way.
XML documents are human readable, and yet there are many applications designed to parse XML documents and work efficiently with their content. PHP5 has new XML-related functions that can easily be used to work with XML documents, or transform non-XML data into XML documents.


You can make your own XML document as easily as this:


<?xml version="1.0" ?>
<php_programs>
   <program name="cart">
   <price>100</price>
   </program>
   <program name="survey">
      <price>500</price>
   </program>
</php_programs>

The first line of this document specifies the version of XML that's being used (notice the delimiters <?xml and ?>—awfully close to PHP's delimiters, so make sure to use the full <?php to start your PHP code). The second line defines the root element of the document (named programs). There can only be one root element for an XML document. The third line defines a child element of the root element, named program, and it contains an attribute named name that is set equal to cart.


From these lines, it should be obvious that this XML document is about PHP programs, and that there are two programs available (cart and survey), and that the price of cart is $100, whereas the price of survey is $500. The root element may contain multiple elements (in the example there are two program elements in the root element), and like HTML, XML documents are composed primarily of elements and attributes.


Another feature of XML documents is that their elements may contain other elements and plain text as content, whereas their attributes may be set directly equal to values. Oh and, it's pretty easy to read, but there are also many parser programs that make it easy to process with programs as well.


Anyone can write XML documents, and many folks also program their applications and programming languages to handle XML documents, both reading existing documents and composing their own new ones. The XML specification is free for anyone to use; the World Wide Web Consortium (at www.w3.org, the same place as the HTML and XHTML specs are maintained) authored and maintains the latest versions of the spec.


Although you can write XML documents just by laying out a few elements and attributes, often you want predefined elements and attributes, so that when you exchange data with another person or application, both parties to the transaction know exactly what the element and attribute names mean. Predefined elements and attributes are specified in a document type definition (DTD) or by an XML Schema, both of which are discussed a little later in this chapter. Frequently you'll find that before you write an XML document, you'll need to either find or write your own DTD or XML Schema. Once you write a DTD, you can publish it on the Web, and anyone who needs to write an XML document compatible with yours (or receive one from you and validate it) has the capability to check the published DTD as he reads your document.