Skip to main content

Wider Page

 

Bigger text

 

In the previous lesson, we discussed the three most common data formats used in networking automation and DevOps worlds. In this lesson, we continue with more detailed lessons on XML - the Extensible Markup Language.

Why do we need XML?

XML is a universal way to move structured information between systems as a plain text data format. In network automation and DevOps, different tools typically speak different "languages." XML is a common data format that most modern programming interfaces can speak. It helps devices and software share data in a predictable form, as shown in the diagram below.

Transferring data between programs
Figure 1. Why do we need XML?

For example, if a Go program wants to collect some data from a Python script, XML can be the glue. It is a simple, human-readable, and machine-friendly format to exchange data. 

Many network protocols and DevOps tools use XML. For example, NETCONF and many vendor APIs return XML. Using XML makes automation more reliable and also makes parsing and validating data easier.

What is XML?

To understand what XML is, let's break down all parts of its name and see why it is called Extensible Markup Language.

Why is it Markup?

XML is called a “markup” language because it is built around the concept of <tags> to describe pieces of information. Tags are words enclosed in angle brackets, like < and >. These tags, along with optional attributes, are written in a text format that follows a clear hierarchy. Because of that structure, XML can be easily processed by almost any programming language.

The following example describes a straightforward document with the basic properties of a network device.

<device>
  <hostname>SW1</hostname>
  <ipAddress>10.1.1.1</ipAddress>
</device>

An XML document is a very flexible construct; it can be nested and extended indefinitely in alignment with the user's needs.

Why is it Extensible?

By default, XML doesn’t come with any built-in tags. You create your own tags to describe the kind of data your document contains. That’s why it’s called extensible — you can extend the basic structure to fit whatever type of information you need to represent.

The following diagram shows how XML is highly extensible. You can scale it vertically by adding more elements at the same level — for example, more devices, interfaces, etc. You can also scale it horizontally by adding deeper nested layers — for example, an interface inside a device, then statistics inside that interface, then directions ingress and exgress, then errors, drops, and so on.

How is XML extensible.
Figure 2. How is XML extensible.

There’s no fixed limit to how many elements or levels you can have. This makes XML powerful for describing both simple data and very complex structures, like a full network configuration with hundreds of interfaces, each having many parameters, settings, and counters.

Why is it a language?

XML is considered a meta-language and a markup language. It allows users to build other languages on top of it, such as RSS (RDF Site Summary), WML (Wireless Markup Language), and XSL (Extensible Style Language).

XML vs. HTML

Many people wonder why we need XML if we already have another very popular markup language like HTML. The answer is straightforward - HTML is designed to be consumed by web browsers and not humans. HTML uses predefined tags/elements and document structure. It is also not extensible with user-defined constructs. XML, on the other hand, is specifically designed to be human-readable and extensible in alignment with the user's needs. Let's look at the following example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 <html xmlns="https://www.w3.org/1999/xhtml"> 
 <head> 
 <title>Interface Configuration</title> 
 <meta http-equiv="Content-Type" 
    content="text/html; charset=utf-8" /> 
 </head> 
 <body> 
 <h1>GigabitEthernet0/0</h1> 
 <h2>Description: Link to Router 1</h2> 
 <p>IPaddress:192.168.1.1</p> 
 <p><b>Mask: 255.255.255.0</b></p> 
 <p><strong>Speed:1000</strong></p> 
 ... 
 <h1>GigabitEthernet0/1</h1> 
 <h2>Description: Link to Router 3</h2> 
 <p>IPaddress:192.168.5.1</p> 
 <p><b>Mask: 255.255.255.0</b></p> 
 <p><strong>Speed:100</strong></p> 
 ... 
 <h1>GigabitEthernet0/0</h1> 
 <h2>Description: Link to Router 4</h2> 
 <p>IPaddress:192.168.43.1</p> 
 <p><b>Mask: 255.255.255.192</b></p> 
 <p><strong>Speed:10</strong></p> 
 </body> 
 </html>

Take a good look at the above example. A human can certainly read this document and make sense of the content, but it is definitely not human-readable and not easy to parse via any programming language. Let's look at another example of similar content but documented with XML instead of HTML.

<?xml version="1.0" encoding="UTF-8"?>
<interfaces>

  <interface id="1">
    <name>GigabitEthernet0/0</name>
    <description >Link to Router 1</description>
    <address>192.168.1.1</address>
    <mask>255.255.255.0</mask>
    <speed>1000</speed>
  </interface>

  <interface id="2">
    <name>GigabitEthernet0/1</name>
    <description >Link to ROuter 3</description>
    <address>192.168.2.1</address>
    <mask>255.255.255.0</mask>
    <speed>100</speed>
  </interface>

  <interface id="3">
    <name>GigabitEthernet0/2</name>
    <description >Link to Router 4</description>
    <address>192.168.43.1</address>
    <mask>255.255.255.192</mask>
    <speed>100</speed>
  </interface>

</interfaces>

Note how simpler it is to read and make sense of the data in the document. Also, think about whether it would be easier to parse the data with a programming language. With XML, you will only need to tell the programming language that all data enclosed in the <interface></interface> brackets belong to this interface. 

The following table compares the two languages.

HTML vs XML comparison
Figure 3. HTML vs XML comparison.

XML does NOT do anything

It is important to understand that the XML below does NOT do anything on its own. It is just information wrapped in tags following the pre-defined set of rules.

<?xml version="1.0" encoding="UTF-8"?>
<interfaces>

  <interface id="1">
    <name>GigabitEthernet0/0/1</name>
    <description >VLAN20</description>
    <address>10.1.1.1</address>
    <mask>255.255.255.0</mask>
    <MTU>1400</MTU>
    <duplex>full</duplex>
    <speed>1000</speed>
  </interface>

  <interface id="2">
    <name>GigabitEthernet0/0/2</name>
    <description >VLAN20</description>
    <address>192.168.1.1</address>
    <mask>255.255.255.128</mask>
    <MTU>1500</MTU>
    <duplex>full</duplex>
    <speed>1000</speed>
  </interface>

  <interface id="3">
    <name>GigabitEthernet0/0/3</name>
    <description >VLAN20</description>
    <address>172.16.5.1</address>
    <mask>255.255.255.192</mask>
    <MTU>1514</MTU>
    <duplex>full</duplex>
    <speed>100</speed>
  </interface>

</interfaces>

This XML data can be fed into algorithms and programming languages. It can be modified and stored in a local file. It can be sent over the network. But on its own, it is just a bunch of clear text.

XML is an Open Standard

XML is stored in a clear-text format. This provides a software- and hardware-independent way of storing, transporting, and sharing data.

Because it is an open standard, XML is widely adopted and supported across many popular applications and web browsers. It is also one of the office formats supported by Microsoft Office, Open Office, and Google Docs.