Solved

parse convert xml feed to text (python)

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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.
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 …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

695 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