Understanding the basic concepts of the XML format and the content it contains will be important when dealing with APIs and writing scripts.
I continue my series in studying for the Cisco DevNet Associate certification.
XML stands for Extensible Markup Language and is a way to describe sets of data. Its purpose is to be human readable and to be used to store and transport data.
When it comes to networking, we can use XML to exchange data between systems and also store data. With the use of APIs, we can query network devices and change their settings.
Basic structure of XML as an example:
<network>
<name>Meraki San Francisco - wireless</name>
<total_usage_kbytes>1097882601473</total_usage_kbytes>
<total_user_count>6680</total_user_count>
<node>
<mac>88:15:44:60:76:48</mac>
<name>1.01</name>
<lat>37.7706845469377</lat>
<lng>-122.387316962811</lng>
<is_active>true</is_active>
<seconds_since_update>11</seconds_since_update>
<channel>1</channel>
<last_reported_from>184.23.135.130</last_reported_from>
<gateway_metric>1</gateway_metric>
<is_gateway>true</is_gateway>
<users_in_last_day>9</users_in_last_day>
<usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
</node>
</network>
It reads straight forward where you have network
as the root element containing child elements such as name
and node
.
What we can do with this data is manipulate it through scripting or software by sending, receiving, or displaying the data.
XML Structure
Think of XML as a tree-like structure. There are different types of nodes but we should be very comfortable with elements and data.
- document node
- elements
- data
- processing instructions
- comments
When we work with XML we must follow some rules so it maintains the correct structure used across systems. It’s rather simple to follow this structure just by looking at the example above.
An XML element must have a start and end-tag enclosed in angle brackets. An example is <Hostname>..</Hostname>
. And it is possible to have an empty element but, again, it should have a start-tag and end-tag.
From the example you can see that some XML elements are nested. When nesting elements, they must be closed properly.
So let’s take a look at that example again:
<network>
<name>Meraki San Francisco - wireless</name>
<total_usage_kbytes>1097882601473</total_usage_kbytes>
<total_user_count>6680</total_user_count>
<node>
<mac>88:15:44:60:76:48</mac>
<name>1.01</name>
<lat>37.7706845469377</lat>
<lng>-122.387316962811</lng>
<is_active>true</is_active>
<seconds_since_update>11</seconds_since_update>
<channel>1</channel>
<last_reported_from>184.23.135.130</last_reported_from>
<gateway_metric>1</gateway_metric>
<is_gateway>true</is_gateway>
<users_in_last_day>9</users_in_last_day>
<usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
</node>
</network>
<name>
is nested under <node>
. Nested elements should be closed prior to the element above it. Additionally, the element names are case sensitive.
If we wanted to add comments we can do so with the syntax <!—- this is a comment -->
<device>
<!--THIS IS A COMMENT-->
<Hostname>R1</Hostname>
<IPv4>192.168.1.1</IPv4>
</device>
Here’s an XML example from the Meraki sandbox:
<network>
<name>Meraki San Francisco - wireless</name>
<total_usage_kbytes>1097882601473</total_usage_kbytes>
<total_user_count>6680</total_user_count>
<node>
<mac>88:15:44:60:76:48</mac>
<name>1.01</name>
<lat>37.7706845469377</lat>
<lng>-122.387316962811</lng>
<is_active>true</is_active>
<seconds_since_update>11</seconds_since_update>
<channel>1</channel>
<last_reported_from>184.23.135.130</last_reported_from>
<gateway_metric>1</gateway_metric>
<is_gateway>true</is_gateway>
<users_in_last_day>9</users_in_last_day>
<usage_in_last_day_in_kbytes>959578</usage_in_last_day_in_kbytes>
</node>
</network>
Leave a Reply