rpratesh
rpratesh

Reputation: 51

Parse XML file in python and display it in HTML page

I am doing a digital signage project using Raspberry-Pi. The R-Pi will be connected to HDMI display and to internet. There will be one XML file and one self-designed HTML webpage in R-Pi.The XML file will be frequently updated from remote terminal.

My idea is to parse the XML file using Python (lxml) and pass this parsed data to my local HTML webpage so that it can display this data in R-Pi's web-browser.The webpage will be frequently reloading to reflect the changed data.

I was able to parse the XML file using Python (lxml). But what tools should I use to display this parsed contents (mostly strings) in a local HTML webpage ?

This question might sound trivial but I am very new to this field and could not find any clear answer anywhere. Also there are methods that use PHP for parsing XML and then pass it to HTML page but as per my other needs I am bound to use Python.

Upvotes: 2

Views: 5696

Answers (2)

rpratesh
rpratesh

Reputation: 51

Instead of passing the parsed data (parsed from a XML file) to specific components in the HTML page, I've written python code such that it rewrites the entire HTML webpage's code periodically.

Suppose we have a XML file, a python script, a HTML webpage.

  • XML file : Contains certain values that are updated periodically and are to be parsed.
  • Python Script : Parses the XML file (when ever there are changes in XML file) and updates the HTML page with the newly parsed values
  • HTML webpage : To be shown on R-Pi screen and reloaded periodically (to reflect any changes at the browser)

The python code will have a string (say, str) declared, that contains the code of the HTML page, say the below code.

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

Then suppose we would like to update the My first paragraph with a value we parsed from XML, we can use Python string replacement function,

str.replace("My first paragraph",root[0][1].text)

After the replacement is done, write that entire string (str) into the HTML file. Now, the HTML file will have new code and once it's reloaded, the updated webpage will show up in the browser (of R-Pi)

Upvotes: 1

Alan
Alan

Reputation: 687

I think there are 3 steps you need to make it work.

  1. Extracting only the data you want from the given XML file.
  2. Using simple template engine to insert the extracted data into a HTML file.
  3. Use a web server to service the file create above.

Step 1) You are already using lxml which is a good library for doing this so I don't think you need help there.

Step 2) Now there are many python templating engines out there but for a simple purpose you just need an HTML file that was created in advance with some special markup such as {{0}}, {{1}} or whatever that works for you. This would be your template. Take the data from step 1 and just do find and replace in the template and save the output to a new HTML file.

Step 3) To make that file accessible using a browser on a different device or a PC you need to service it using a simple HTTP web server. Python provides http.server library or you can use an 3rd party web server and just make sure it can access the file created on step 2.

Upvotes: 1

Related Questions