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

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

This series of articles discusses the use of XML on applications for the Raspberry Pi. Part One covered what is XML and the format of the data structures, and Part Two covered building and parsing XML in Python. In Part Three we  see how XML is used as 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

 

The Application – RasPiConnect

What is XML being used for in this program?

RasPiConnect
RasPiConnect Control Panel

XML is being used for three purposes in this program. 1 ) For the communications channel (over HTTP) from the App to the Raspberry Pi. 2) For the communications channel from the Raspberry Pi to the App (over HTTP) and 3) For persistent program and screen configuration storage in the App.

The Communications Channels

The Client communicates with the Server by sending and receiving HTTP. The Objective C code on the Apple iOS device for sending and receiving is beyond the scope of this article. The Python code on the Server for receiving and sending XML code is very straight forward. In the Server we use the web.py library from webpy.org. This is a light weight webserver program readily available by running the following command on your Raspberry Pi.

  sudo apt-get install python-webpy

There is a bi-directional communication channel between the Server and Client. Both directions are handled by HTTP requests. The Client sends requests for data and action with all the requests grouped together. The XML sent from the Client to the Server looks like this:

  <XMLObjectXMLRequests>
   <XMLCOMMAND>
    <OBJECTSERVERID>LT-1</OBJECTSERVERID>
    <OBJECTNAME>CPU Text and Label
    </OBJECTNAME>
    <OBJECTTYPE>1</OBJECTTYPE>
     ...
   </XMLCOMMAND>
    ...
   </XMLCOMMAND>
  </XMLObjectXMLRequests>

This XML contains multiple requests to the Server for retrieving information and sending action requests to the Server. Note the multiple <XMLCOMMAND> entities in the structure.

The returning XML from the Client looks very similar.

  <XMLRESPONSES>
   <XMLCOMMAND>
    ...
   </XMLCOMMAND>
   <XMLCOMMAND>
    <OBJECTSERVERID>LT-1</OBJECTSERVERID>
    <RESPONSE>
     <![CDATA[43.31, 43.31, CPU Temp (deg
 C)]]>

</RESPONSE>

    ...
   </XMLCOMMAND>
  </XMLRESPONSES>

Parsing the XML

Parsing this XML into the individual entities (<XMLCOMMANDS> above), is a simple use of the ElementTree Python library as shown previously in Part Two of this article. Once the requests have been parsed and validated, the server executes the requests one at a time, while building a new XML structure containing the responses to the commands. The <XMLResponses> structure is then sent to the Client using one HTTP connection rather than multiple connections.

 ...
 class RasPi:
     def POST(self):
         web.header('Content-Type',
 'text/html')
         incomingXML = web.data()
         root = ET.fromstring(incomingXML)
         # iterate through all the values
         for element in
 root.findall('XMLCOMMAND'):
 ...

Building XML to send to the Client

Building the XML to be sent back via the HTTP request from the Client to the Server is done by constructing a string of concatenated XML commands and then returning the string as part of the web.py POST HTTP request.

   # start of building the XML responses
   outgoingData="<XMLRESPONSES>"
     ...
     outgoingData +="<XMLCOMMAND>"
      ...
      outgoingData +="</OBJECTTYPE>"
      outgoingData +="<OBJECTID>"
      outgoingData += "%i" % objectID
      outgoingData +="</OBJECTID>"
      # done with FOR loop
   outgoingData+="</XMLRESPONSES>"
   return outgoingData

Receiving and sending the XML

In web.py, the incoming XML is placed in a string as above and then parsed. The responses are sent back to the client from the POST function by returning a string.

Conclusion

XML is a very useful means for storing and transmitting data across disparate computer systems. It is usable by large and small i-love-xml-280x300computers alike. To learn more about using XML on a Python based platform try the following websites:

https://docs.python.org/2/library/xml.etree.elementtree.html

https://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree

and a tutorial video on elementtree: https://www.youtube.com/watch?v=LNYoFo1 sdwg