XML – Proper formatting, components explained via IOS config and small “Awesome Food” Data File, and XML DOM (Document Object Model) review!

XMLDemo1

Above is an example of “sh run | format” XML output from R1 from my Auto Lab!

In every Cisco IOS device “sh run” output comes in the format of ASCII by default for human readability (thank you Cisco), however its native “computer readable” format is XML which is shown in the above graphic which I will dissect and explain in this article.

I have pasted the “sh run | format” into Visual Studio Code for this as well, as I feel like the colors that match for certain parameters really helps visualize the concepts!

I’ll begin with a few extremely easy concepts to get with XML coding format right away

XMLDemo2

Line 3 = XML Prolog = The “Header” to the Data Format which defines the XML Version, the Encoding type (usually UTF-8), etc

Line 4 = XML Tags = <something> <value> <value> <value> </something> which in the context of line 4, this is referring to the IOS Version #, and also actually demonstrates another good concept right to roll into which is Tags within Tags.

Much like you would type [b] Text you want to be in bold [/b] which is BB Code for forums that I would imagine most people have run into, this note where the “XML Tag” begins and ends, so you cannot have.

So what is allowed with Tagging inside Tagging that is allowed? Good Question!

The actual output of Line #4 code gives a perfect example of how it can be done:

<version><Param>15.6</Param></version>
Going back to my BB Code example as I feel its very familiar to forum dwellers, you end the BB “tag” to change your typing style and re-start, or you embed a style in it, example:
[b] This is all going to be BOLD text [i] this text will be bold and in Italics [/i] and then the rest of this is just bold text! [/b]
If I wanted the Italics text to not be bold, I would type it out:
[b] This is going to be bold text [/b][i] and this will be non-bold Italics [/i][b] and the rest will be bold again![/b]
Now had I missed a [/] to close that or even a [] to begin it somewhere, it would not be recognized by the forum code, which I am sure we have all done and tried to speed edit so no one caught us trying to one up them and making a typo!
We all know we are guilty of this πŸ™‚
This exact same topic applies to XML Tags, this example wouldn’t be understood by XML:
<sometag1> value <sometag2> anothervalue </sometag1> </sometag2>
This will not work, you can have XML Tags within Tags, however EXACTLY LIKE BB CODE(!!!) THE VALUES ASSOCIATED IN THE TAG MUST STAY WITHIN IT!
Using that example if we formatted it this way, no problem:
<sometag1> value <sometag2> anothervalue </sometag2> </sometag1>
Point being – The “Parent Tag” cannot be closed before the inner tags close!
Otherwise the “Tags” themselves within the <> are pretty flexible with accepting alpha-numeric characters of either Upper or Lower case, and values appear flexible too:
XMLDemo3
First Line #33 knocks my point out of the park with my possible confusing explanation:
<ip><address><IPAddress>192.168.238.111</IPAddress><IPSubnetMask>255.255.255.0</IPSubnetMask></address></ip>
The Parent Tag being “ip” with several tags within it, but they all close in an order that makes logical sense, as the IP Address and Subnet Mask are both components of IP.
There is also a little mystery tag thrown in there that I had to lookup a bit:
<speed><auto/></speed>
<media-type><rj45/></media-type>
WHAT IS THAT / IN THE MIDDLE OF MY TAGS DOING TO MY LOGIC HERE??!??!
These values within tags actually indicate that it is an empty value, it does still have a value of “auto” duplex and “rj45” media type, but it has no output value beyond that.
That had me scratching my head, so there it is πŸ™‚
Now onto my own Awesome Food XML File I wrote to a couple other concepts!
XMLDemo4
Because what else is worth writing an XML File for? Can you see what is wrong with this File specifically? I didn’t notice it until I pasted the screen snip here.
……..
…………
………..
I didn’t close the Tags for any of the actual food categories before starting a new one!
Ice Cream Pizza Fries might be awesome, but I better adjust my code to be formatted correctly here, and I will also highlight the couple more things to discuss:
XMLDemo5
I cannot believe after preaching that til I was blue in the face, I completely whiffed the ball on that first swing at scripting XML, but we learn through our mistakes! πŸ™‚
The very poorly drawn red brackets in this file are the “XML Elements” within it, whereas this file is regarding AwesomeFood, the Elements are the different types of Awesome Foods, and then it goes into Flavors / Toppings / Etc.
I did draw the brackets on both side for clarity.
Attributes are the last thing I wanted to touch on, I am honestly not entirely sure what purpose they serve as of yet in my studies, but I underlined them in red here.
These are actually additional ways to “ID” an element (Attributes always reside within Elements to give them additional detail / naming), so for example in my file I just called every “Food Id=100” “Food Id=200” instead of naming it IceCream and Pizza, then I could have a mechanism (the Attribute) to tell them apart.
NOTEΒ  – The Attribute does not have to be “Id” and the value doesn’t have the be a #, however it must be in either single or double quotes on both sides!
While on this topic I’d like to explore XML DOM when looking at this file
Wanted to quickly touch on this pretty straight forward concept of DOM, and how it essentially treats the “Root” of a collection of “Elements” are a Parent, and the Elements to that Parent / Root are its Children – And of course Elements of the same Parent would therefor be Siblings to each other (as long as they have the same Parent / Root)!
To illustrate XML DOM (Document Object Model) / Tree Structure I drew this up:
XMLDom
Pieces of XML File can be referenced by Siblings of Parents, which when looking at my short “AwesomeFood” code, it would look like the above Tree Structure using XML DOM.
AwesomeFood is the parent, all three food types are siblings to the parent awesome food, and are also siblings to each other because they come from the same parent!
So XML DOM is not a difficult concept, however you must know that each “Element” is considered a “Child” of its “Parent” and multiple Elements under the same “Parent” can be considered XML “Siblings” – I would expect to see this on the exam!
I would open VSC and find some longer XML scripts to load into it, and practice identifying the siblings to parents, as I believe this may be on the blueprint, and if it isn’t I would still be prepared to identify siblings within an XML Data Format on exam day!
Anyhow, back to the Attributes and working with the single or double quotes!
For example if an apostrophe is needed in list of Elements like Comedians and one ID was Comedian = “Conan O’Brian” then we can use that apostrophe without it getting mixed up within our quotations identifying Conan O’Brian as … semi funny.. kind of.
Another way to add an apostrophe using code to do so (I would know this for exam day):
XMLDemo7
By adding &apos; right where the apostrophe should go will place one there!
That may be a better way to do it just to avoid having to troubleshoot that in the File down the road some day, or maybe just use ” ” in user ID and save the ‘ for apostrophes.
Now for my final presentation of XML – Transforming my favorite foods to JSON!
Using the website https://codebeautify.org/xmltojson which converts XML to JSON, I have converted my totally legit functioning XML Data Format to JSON format below(!!!) :
XMLDemo8
And into JSON:
XMLDemo9
Technology facilitates my stupidity way too easily some boring nights! πŸ™‚
And with that I will end my XML Formatting review here, I will come update this article if I get any new info or use cases, otherwise onto either JSON or YAML next!
Until next time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s