Solved

parse convert xml feed to text (python)

Posted on 2016-11-13
2
102 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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to share My.Settings in multiple application instances ? 7 54
Need a modeling tool 2 43
UM7 Stop Sending packets with GPS Sensor 1 21
xml with php question 5 37
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

726 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