Python Forum
XML parsing and generating HTML page Python 3.6 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: XML parsing and generating HTML page Python 3.6 (/thread-12421.html)



XML parsing and generating HTML page Python 3.6 - Madhuri - Aug-24-2018

is it possible to parse the XML file and generate the html page in python 3.6
i want extract the data from XML and put it needed info into html page


RE: XML parsing and generating HTML page Python 3.6 - Larz60+ - Aug-24-2018

Answer is yes, using lxml (or BeautifulSoup):

this isn't the cleanest code in town, but it uses lxml to parse XML
example: https://www.scrapehero.com/how-to-scrape-business-details-from-yellowpages-com-using-python-and-lxml/


RE: XML parsing and generating HTML page Python 3.6 - snippsat - Aug-24-2018

(Aug-24-2018, 05:06 AM)Madhuri Wrote: is it possible to parse the XML file and generate the html page in python 3.6
i want extract the data from XML and put it needed info into html page
Yes it possible,it depend of the HTML is already generated and have a server running.

Here a example that parese XML and generate HTML using jinja2.
I use jinja2 with Flask(jinja2 is build in) for sending stuff from server to HTML.
jinja2 can also work alone as shown here.
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader

xml ='''\
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>'''

# parse
soup = BeautifulSoup(xml, 'xml')
mes_from = soup.find('from').text

# Generate html
# test.html
#<h1>{{ message }}</h1>
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('test.html')
output = template.render(message=mes_from)
print(output)
Output:
<h1>Jani</h1> # If remove .text from parsing <h1><from>Jani</from></h1>