?
Solved

parse convert xml feed to text (python)

Posted on 2016-11-13
2
Medium Priority
?
185 Views
Last Modified: 2016-11-15
hi i am new to python and need to produce a text file based on the categories in  http://m.highways.gov.uk/feeds/rss/AllEvents/East%20Midlands.xml

I have read loads and all fail. so back to basics...

ideally i need <category> , <title> , <road> ,<eventEnd>


code so far and failing

#!/usr/bin/python2.6
# Copyright: robert@roberteklund.org 2012
# East Midlands.xml


from xml.etree import ElementTree

with open('East Midlands.xml', 'rt') as f:
    tree = ElementTree.parse(f)

for node in tree.iter():
    title = node.attrib.get('title')
    category = node.findtext('category')
print '  %s' % (title)
    

Open in new window

0
Comment
Question by:James Murrell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 2000 total points
ID: 41885835
There is a package just for parsing RSS feeds, which is much simpler to use (RSS is afrom of XML). This code worked for me:

import feedparser

d = feedparser.parse('http://m.highways.gov.uk/feeds/rss/AllEvents/East%20Midlands.xml')
 
for post in d.entries:
    print(post.title + ' :' + post.category + ' : ' +  post.road) 

Open in new window


Output:
M1 southbound between J22 and J21A | Southbound | Road Works :Road Works :M1
link road from M1 J23A to A453 northbound | Northbound | Road Works :Road Works :M1
link road from M1 J23A to A453 northbound | Northbound | Road Works :Road Works :M1
A42 northbound between A453 and M1/A453 | Northbound | Road Works :Road Works :A42
...


You may need to install the feedparser package to your system, either using pip or download it here https://pypi.python.org/pypi/feedparser

I'm using Python 3 so print statement requires brackets. If you are on version 2, then just remove the brackets.
0
 
LVL 31

Author Closing Comment

by:James Murrell
ID: 41888498
Fantastic   thanks
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Introduction to Processes
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question