Introduction

XML, or Extensible Markup Language, rounds out the usual suspects of YAML, JSON, and XML. It’s probably my least favorite of the three, but knowledge of XML is needed when working with code.

XML is, of course, related to HTML. So why didn’t we just settle for HTML? Turns out machines don’t understand HTML very well. They can parse it perfectly fine, yes, but in HTML you put information in, such an address, and you understand it’s an address because you are a human. A machine doesn’t know that Baker Street is an address, unless you tell it.

Syntax

XML consists of tags, elements, and attributes. Let’s take a basic example and then go through these in more detail:

<?xml version="1.0" encoding="UTF-8"?>
<address>
  <name>
    <title>Mr</title>
    <first-name>Sherlock</first-name>
    <last-name>Holmes</last-name>
  </name>
  <street>221B Baker Street</street>
  <city state="NA">London</city>
</address>

First, we declare that this is an XML document and the encoding used. This is called a prolog. It’s optional, but if included, should always be the first line.

The tag <address> is the root of the document. We must always have a root. The tag <address> has three children:

  • <name>
  • <street>
  • <city>

The tag <name> has three children as well:

  • <title>
  • <first-name>
  • <last-name>

As mentioned before, we have tags, elements, and attributes in XML:

  • A tag is the text between <>, such as <name>
  • An element is the starting tag, ending tag, and everything in between. Again, using <name> as an example, it would be everything from line 3 to line 7
  • An attribute is a key/value pair inside the starting tag of an element. Notice that state was part of <city state="NA"> element, rather than having state as its own tag

XML documents must be contained in a single element, the root element. The following document would then not be valid:

<?xml version="1.0" encoding="UTF-8"?>
<address>221B Baker street</address>
<address>10 Downing Street</address>

Furthermore, these rules must also be abided:

  • End tags are required. You can’t have a tag <name> and then not ending it with </name>
  • XML elements are case sensitive. Having a starting tag of <name> and end tag of </NAME> is not valid
  • Attributes must have values
    • The values must be enclosed within quotation marks
  • That means that having <city state> is not valid but <city state="NA"> is

We may want to use comments in our documents. So how do you do that? To start a comment, you use <!-- and then you end it with --> such as <!-- This is a comment -->

Comparison to JSON

In the previous post, we learned about JSON. It can be useful to compare one of the examples there, and then try to convert it to XML:

{
  "src_name": "App",
  "dst_name": "Web",
  "whitelist": [
    {
      "port": [0, 0],
      "proto": 1,
      "action": "ALLOW"
    },
    {
      "port": [80, 80],
      "proto": 6,
      "action": "ALLOW"
    },
    {
      "port": [443, 443],
      "proto": 6,
      "action": "ALLOW"
    }
  ]
}

This is what it could look like in XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <src_name>App</src_name>
  <dst_name>Web</dst_name>
  <whitelist>
    <port>0</port>
    <port>0</port>
    <proto>1</proto>
    <action>ALLOW</action>
  </whitelist>
  <whitelist>
    <port>80</port>
    <port>80</port>
    <proto>6</proto>
    <action>ALLOW</action>
  </whitelist>
  <whitelist>
    <port>443</port>
    <port>443</port>
    <proto>6</proto>
    <action>ALLOW</action>
  </whitelist>
</root>

Finally, let’s look at an example from a CSR:

devasc#show ip interface brief | format
<?xml version="1.0" encoding="UTF-8"?>
  <ShowIpInterfaceBrief xmlns="ODM://built-in//show_ip_interface_brief">
    <SpecVersion>built-in</SpecVersion>
    <IPInterfaces>
      <entry>
        <Interface>GigabitEthernet1</Interface>
        <IP-Address>192.0.2.1</IP-Address>
        <OK>YES</OK>
        <Method>manual</Method>
        <Status>up</Status>
        <Protocol>up</Protocol>
      </entry>
      <entry>
        <Interface>GigabitEthernet2</Interface>
        <IP-Address>192.0.2.129</IP-Address>
        <OK>YES</OK>
        <Method>manual</Method>
        <Status>administratively down</Status>
        <Protocol>down</Protocol>
      </entry>
      <entry>
        <Interface>GigabitEthernet3</Interface>
        <OK>YES</OK>
        <Method>unset</Method>
        <Status>administratively down</Status>
        <Protocol>down</Protocol>
      </entry>
      <entry>
        <Interface>GigabitEthernet4</Interface>
        <OK>YES</OK>
        <Method>unset</Method>
        <Status>administratively down</Status>
        <Protocol>down</Protocol>
      </entry>
    </IPInterfaces>
  </ShowIpInterfaceBrief>

Let’s go through this information line by line:

Line 2 is the prolog and indicates version 1.0 of XML as well as UTF 8 encoding

Line 3 is the start of the root element <ShowIPInterfaceBrief>, and name space is used, as indicated by xmlns, and the URI that follows

Line 4 and 5, <SpecVersion> and <IPInterfaces>, are children of <ShowIpInterfaceBrief>, and indicate version and that these are IP interfaces, respectively

Line 6 is the start of the first <entry> and it has the following children:

  • <Interface> indicating the name of the interface
  • <IP-Address> indicating the IP address of the interface
  • <OK> indicating if the interface is OK
  • <Method> indicating if IP was derived by setting it manually or through the use of DHCP
  • <Status> indicating if admin state is up or down
  • <Protocol> indicating if line protocol is up or down

The first <entry> ends at line 13 and then at line 14 the next one starts

Line 35 is the end of the final <entry>

Line 36 is the end of all <IPInterfaces>

Finally, line 37 is the end of the root element <ShowIpInterfaceBrief>

I hope this post will help you get started with XML. Happy studies!

DevAsc – Introduction to XML
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *