XML technologies / XML / Python & XML / Getting started with Python and XML / Key advantages of XML

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

XML has several important advantages that make it the Internet’s leading data representation language. Built into XML from the start, these advantages are precisely what attracts Internet developers.

Application independence

First, XML is readable by both people and machines, a significant point. Have you ever tried reading a Microsoft Word document in a text editor? You cannot if it was saved as a .doc file: information in a .doc document is binary, readable only by computers, although most Word documents mainly contain text. No application other than Word can use it unless taught Word’s complex binary format. It must also learn to handle format changes whenever Microsoft releases a new version.

This sounds daunting to developers, but how hard is it really? Word is extremely popular, so surely its format cannot be too difficult to work out. Examine the beginning of the Word file containing this chapter:

ÐÏ_ࡱ_á             >  _ þÿ     _           _   B _      _ D_   _
 þÿÿÿ       ? _   @_   A_  ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿì¥Á 7              _  ð_¿      _      _  ›_ _
 bjbjU_U_                                     __ 0¸_ 7| 7| W_  _           C
 ÿÿ_         ÿÿ_        ÿÿ_                   l Ê_
 Ê_   Ê_         Ê_         Ê_       Ê_     Ê_        ¶       Þ_

Anyone who has opened a Word file in a text editor will recognize this. Our text, the actual content, is invisible, presumably buried deep inside the file. Finding the real content and its location can be hard, but should not be: these are our data. Try another supported format, Rich Text Format or RTF. Unlike the .doc file, this is text-based and should be easier to decipher. Search the file for the beginning of our text:

  \par }\pard \s34\qr
 \li0\ri0\sb80\sa480\sl240\slmult0\widctlpar\aspalpha\aspnum\faauto\out
 linelevel0\widctlpar\aspalpha\aspnum\faauto\outlinelevel0\pnrauth1\pnr
 date-967302179\pnrnot1\adjustright\rin0\lin0\itap0 {\b0\fs48 Python un
 d XML}{
 \b0\deleted\fs48\revauthdel1\revdttmdel-2041034726
 Grundlagen}{\b0\fs4
 8\revised\revauth1\revdttm-2041034726 ?}{\b0\fs48 
 \par }\pard\plain \qj  

Better: the chapter title is visible, giving us a starting point to decipher the structure. The markup looks complex and includes an older chapter title. Extracting the text we actually want requires understanding Word’s version-control model, which still presents many challenges.

XML, by contrast, is application-independent. An XML document is normally handled by an XML parser or processor, but is easily read and parsed without one. XML data is not confined to a particular application. Reading rich files may be invaluable when, in 20 years, you unearth a CD-ROM of old business forms you suddenly need. Will QuickBooks still let you extract them in 2021? XML data can be read with any text editor.

Now examine this chapter in XML. Its markup comes from a document type for software manuals and documentation (DocBook), making it somewhat verbose; it contains no version information, but the text is now easy to identify:

<chapter>
    <title>Python und XML</title>
    <para>Python und XML sind zwei sehr verschiedene Dinge, jeweils mit einer eigenen, reichen Geschichte. Python ist eine universale Programmiersprache, die durch die Vision und unter der Leitung ihres Erfinders Guido van Rossum sehr organisch aus ihren ...</para>
    ...
</chapter>

Notice the additional characters outside the document content: markup, or tags. We saw these in RTF too, alongside much hard-to-understand text; presumably Word’s strange binary data represents something similar. If this book covered RTF, you would soon infer two things: RTF resembles a printer protocol language much more than this XML does, and writing an RTF reader would be difficult. This book will show how XML defines languages suited to your application, and how easy it is to write XML-reading programs, especially with Python.

Hierarchical structure

XML is hierarchical and lets you choose tag names, unlike HTML. You can freely create any element types and nest other elements inside them. Consider an address record:

<?xml version="1.0"?>
<address>
  <name>Bubba McBubba</name>
  <street>123 Happy Go Lucky Ln.</street>
  <city>Seattle</city>
  <state>WA</state>
  <zip>98056</zip>
</address>

In this well-formed XML, I invented record names and combined them with data. XML software such as a parser, which interprets a document’s syntax, could present the data in different ways because it recognizes the structure. From an application programmer’s perspective, we could turn the record into an object initialized as follows:

addr = Address( )
addr.name = "Bubba McBubba"
addr.street = "123 Happy Go Lucky Ln."
addr.city = "Seattle"
addr.state = "WA"
addr.zip = "98056"

XML is therefore suitable for many serialized objects, though less so for some structures, such as large numerical datasets from scientific computing. Its hierarchy makes object interfaces easy to apply to documents: mapping element names to object types lets application-specific objects be built directly from the information stream. Later we will see XML model more than simple hierarchies.

Platform independence

XML is also cross-platform. This largely follows from its textual format, while defined encodings eliminate ambiguity about document layout. An XML purchase order can easily travel from Unix to a wireless PDA. XML was designed for existing Internet infrastructure, including HTTP, SSL and evolving messaging protocols, making it suitable for distributed applications. It has underpinned message queues, instant messaging and remote procedure call frameworks. We explore these uses further in Python, web services and SOAP and Python and distributed systems design. The example document above is thus more than application-independent: it transfers between computer types without information loss. A programmer could write a technical-book chapter on a favorite Unix system and send it to a publisher using Macintosh typesetting software, avoiding many difficult format conversions.

International language support

As the Internet permeates daily life, we increasingly recognize the world’s cultural richness and diversity. As technologists, however, we are still making software usable in several languages simultaneously. Making text routines “8-bit clean” is no longer sufficient, or even close.

Standards bodies worldwide devised ways for computers to exchange text in their national languages, sometimes several competing ways with varying acceptance. Unfortunately, applications rarely specify the language or interchange format of their data, hindering exchange across the cultural and linguistic boundaries of these standards. Multiple prominent standards can even hinder exchange within one such boundary.

These problems arise from profound cultural differences in handling text. Besides this book’s Western European left-to-right, top-to-bottom style, many writing systems exist. Right-to-left is common, and China uses top-to-bottom lines arranged right-to-left. Hebrew runs right-to-left but writes numbers with Arabic digits left-to-right. Other systems allow annotations alongside text. Imagine combining several writing systems in one document!

Standards bodies have understood these problems and worked on solutions for years. XML’s authors wisely avoided inventing new answers to most of them, instead building on expert work and existing standards.

The International Organization for Standardization (ISO) and the Unicode Consortium developed a standard that, though imperfect, may be the best available: it unifies the world’s text representations to cover every language and alphabet, including ideographic and hieroglyphic character sets. The standard is ISO/IEC 10646, better known as Unicode. Not every national standards body, particularly in Asia, accepts Unicode as the standard for all future text interchange, but many consider it the best available compromise. It addresses bidirectional text, case rules and encoding algorithms guaranteeing various stream properties. It deliberately excludes linguistic issues beyond individual characters. Language-sensitive software may still need considerable work beyond Unicode to compare names correctly in one or more languages. Some languages require substantial extra rendering support, such as Arabic’s different letter forms depending on word position and neighboring letters.

The World Wide Web Consortium (W3C) took a simple, clever step to support both older interchange standards and Unicode. It required every XML document to use Unicode and describe its own encoding so every XML processor can determine it. All processors must recognize certain encodings, making it possible to produce universally readable XML representing all characters. A feature also marks document content with its language, though it is currently used less often than it could be.

Since XML documents are Unicode documents, they support the world’s languages. XML’s use of Unicode and encodings is explained further in XML basics. Unicode strings have been part of Python since version 2.0, and its standard library supports many encodings.

  

 

 

 

back to top