Chapter 10. Alternative approaches to XML programming

Table of Contents

10.1. XML processing with Ruby
10.1.1. DOM parsing using Ruby
10.1.2. SAX parsing using Ruby
10.1.3. Creating an XML document using Ruby
10.2. XML processing with Python
10.2.1. DOM parsing using Python
10.2.2. SAX parsing using Python
10.2.3. StAX parsing using Python
10.2.4. Creating an XML document using Python
10.2.5. Other means of dealing with XML documents using Python
10.3. XML processing with PHP
10.3.1. DOM parsing using PHP
10.3.2. SAX parsing using PHP
10.3.3. StAX parsing using PHP
10.3.4. Creating an XML document using PHP
10.3.5. Other means of dealing with XML documents using PHP
10.4. XML processing with JavaScript
10.4.1. DOM parsing using JavaScript
10.4.2. Creating an XML document using JavaScript
10.5. XML processing with Swift
10.5.1. DOM parsing using Swift
10.5.2. SAX parsing using Swift
10.5.3. Creating an XML document using Swift
10.6. XML processing with E4X
10.6.1. DOM parsing using E4X
10.6.2. Creating an XML document using E4X
10.7. XML alternative notations
10.7.1. JSON
10.7.2. YAML
10.8. Additional information on alternative approaches

In the previous sections, XML was used as a formalism for data organization and markup. It was processed either with XML tools or using the Java programming language, which provides a fairly complete set of tools for processing XML documents. Since this tree based approach to data organization is now well accepted, tools for XML file parsing and creation have been developed for other programming languages. XML packages exist for C++, C#, Perl and even Fortran with capabilities and programming interfaces similar to the ones we have presented for Java. XML thus serves as convenient exchange language between programs written in different programming languages.

Of particular interest is libxml2 library [73] which is an XML parser and toolkit written in C with a variety of language bindings for other programming languages. It is very portable and can be run on most systems. It is most often accessible from the command line thru the xmllint command. The parser can validate with DTD, XML Schema, Relax NG or Schematron. XPath and XSLT are also implemented but at the 1.0 level. Some features of XSLT 2.0 have been implemented in a separate project [74].

We will first present how XML data can be processed in Ruby[44], a dynamically typed scripting language known for its flexibility and its large library of tools for creating Web-based applications. We then show how XML can be processed with Python [32], a popular script language, PHP [76], a server-side script language for the Internet, JavaScript [68] used in web browsers, and Swift [85], , a statically typed script language. For mainly historical purposes, we also describe EcmaScript for XML (E4X).[66],

We will illustrate the use of XML within these programming languages using the same compact and expand applications that we have used in previous chapters. This will allow an easier comparison between different programming languages.

Even though we have argued until now that XML is the ideal markup language, we will describe in this chapter some alternative approaches for a tree based data organization. It should be remembered that most of these simplified XML packages do not provide validation for their input, which must be performed by other means. In the last section of this chapter, we will show other XML competitors for exchanging data between programs: JavaScript Object Notation (JSON) [71] and YAML (YAML Ain't a Markup Language) [90]. These notations are geared toward the production of a human-readable notation for describing tree based informations compared with XML. This goal is achieved but they do not provide any notion of validation or transformation tools within the exchange language itself. But in the context of simple information transfers betweens programs (e.g. between a browser and a Web server using the AJAX technology for a more pleasant interaction) not needing sophisticated validation or transformation, they provide interesting lightweight alternatives.