XML Tutorial – Learn to use for the Raspberry Pi! #1

XML Tutorial – Learn to use this important language for the Raspberry Pi!
2000px-Text-xml.svg

This series of articles will discuss the use of XML on applications for the Raspberry Pi. Part One covers what is XML and the format of the data structures. Part Two will cover building and parsing XML in Python and Part Three will show how XML is used as a communications protocol for a client / server application, RasPiConnect. RasPiConnect is an iPad/iPhone app that connects and displays information for any number of Raspberry Pi’s via a defined XML interface.

XML Tutorial – Learn to use this important language for the Raspberry Pi!  Part 1

XML Tutorial – Learn to use this important language for the Raspberry Pi!  Part 2

XML Tutorial – Learn to use this important language for the Raspberry Pi!  Part 3

 

What is XML?

XML stands for eXtensible Markup Language. It is a language to structure, store and transport information in a hardware and software independent way. It kind of looks like HTML but it is used to transport information not to display information. HTML and XML are both examples of an SGML (Standard Generalized Markup Language).

What do you use XML for?

It is a little difficult to understand, but XML does not “Do” anything. XML is designed to transport information unlike HTML which is used to display information. You use XML to structure data (usually in a human readable format) and to send this data to other pieces of software on your own machine or across the Internet. Often user preferences or user data is also stored in XML and then written to files. If you need to send structured data, then XML is an excellent choice. It is easy to parse, easy to modify, and most importantly, easy to debug. One very useful characteristic of XML files is that they can be extended (more elements, attributes, etc.) without breaking applications. Providing, of course, those applications are well written (see Part Two of this series).

Here is a complete XML message:

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <XMLCOMMAND>
     <OBJECTID>12</OBJECTID>
     <OBJECTSERVERID>BL-1</OBJECTSERVERID>
     <OBJECTTYPE>2048</OBJECTTYPE>
     <OBJECTFLAGS>0</OBJECTFLAGS>
     <RASPICONNECTSERVERVERSIONNUMBER>2.4
     </RASPICONNECTSERVERVERSIONNUMBER>
     <RESPONSE>
         <![CDATA[100.00, 0.00, CPU Load]]>
     </RESPONSE>
 </XMLCOMMAND>

Structure of an XML message

Unlike HTML, in XML you define your own tags. A well formed XML message has a “root” and then “branches” and “leaves”.

The first line is the XML declaration. It rarely changes. The second line describes the root element of the XML document.

<XMLCOMMAND>

XML special character secrets

Note that the end of the XML root has a closing tag:

 </XMLCOMMAND>

All elements in XML must have an opening and closing tag. This, in addition to the root is the definition of a “well-formed XML document”. By the way, all tags in XML are case sensitive. A good XML coding practice is to make all of the tags uppercase. Doing this also makes the structure of the XML stand out when you read it.

Add child elements

xmlChild elements are used to provide additional data and information about the enclosing XML element (i.e. <XMLCOMMAND> in the example above). Note that XML does not require the same set of child elements for each enclosing XML element, making upgrading or changing your elements easy. However, your parser does have to handle this situation!

Child elements are XML elements underneath the root (OBJECTID, OBJECTSERVERID, OBJECTTYPE, OBJECTFLAGS, RASPICONNECTSERVERVERSIONNUMBER, RESPONSE). All of these tags must have a beginning and ending tag similar to the root. In addition, all elements can have child elements nested inside.

XML attributes

XML elements can have attributes, just like HTML. Attributes provide additional information about an element. By convention, attributes are usually given in lower case.

<PICTURE id=”1″ type=”gif” file=”BPNSCFA.gif”> </PICTURE>

It is good practice to use attributes in XML sparingly and in a consistent manner. You can rewrite the above XML as the following:

 <PICTURE id="1">
     <TYPE>gif</TYPE>
     <FILE>BPNSCFA.gif</FILE>
 </PICTURE>

Not having attributes makes the parsing of the XML easier in many ways.

There are two characters that are not allowed inside of an XML element. They are the “<” and “&”. The “>” character is allowed, but it is also good practice to replace this character. The pre-defined entity references in XML for these characters are “&lt;”, “&amp;” and “&gt;”.

Sending special data in XML

Sometimes you want to send general data in your XML element without replacing special characters. For example, you might want to send an HTML page inside an XML element (the RasPiConnect application does this) and you don’t want to change all the characters. XML parses all text inside elements by default, but there is a way to change that: CDATA. Inside a CDATA structure, the XML parser ignores the data and it can be passed without change in an XML message. CDATA looks like this:

 <![CDATA[<XML & DOES & NOT <LIKETHIS>]]>

Validate your XML

There are many sites on the web that will validate that your XML is well formed. https://www.xmlvalidation.com  is one such site. Cut and paste the XML from the first page to try it out.

Coming in Part Two and Three

Part Two of this series will describe simple ways of generating and parsing XML in Python on the Raspberry Pi. Part Three will look at an actual application and how the same XML is used on both the iPad and directly on the Raspberry Pi.

Conclusion

XML is a simple, easily understood method for sending information in a hardware and software independent manner. The main XMLadvantages of XML are readability and portability between systems. It provides an easily extensible framework for information interchange.

To learn more about XML try the following websites:

https://www.w3schools.com/xml/

https://www.quackit.com/xml/tutorial/

1 Trackback / Pingback

  1. XML Tutorial - Learn to use for the Raspberry Pi! #2 - SwitchDoc Labs

Comments are closed.