how to print the list of strings from xml tags
#1
Hello All,

I'm working on my python script to print the list of strings from my own xml file.

I'm trying to find out how to print the list of strings from the programme-channel tag to get the format in the start="" and stop="" tags. When I'm trying to get the list of format and the title, I will only get the list of channels which you can see it here:

Code:
15:00:58 T:3316  NOTICE: 101 ABC FAMILY
15:00:58 T:3316  NOTICE: 102 CBS
15:00:58 T:3316  NOTICE: 103 CNN USA
15:00:58 T:3316  NOTICE: 105 ESPN USA
15:00:58 T:3316  NOTICE: 106 Fox News
15:00:58 T:3316  NOTICE: 107 Animal Planet
15:00:58 T:3316  NOTICE: 108 USA Network
15:00:58 T:3316  NOTICE: 110 SPIKE
15:00:58 T:3316  NOTICE: 111 BRAVO USA
15:00:58 T:3316  NOTICE: 112 BRAVO1
15:00:58 T:3316  NOTICE: 113 BRAVO2
15:00:58 T:3316  NOTICE: 114 BRAVO3
15:00:58 T:3316  NOTICE: 115 BRAVO4
15:00:58 T:3316  NOTICE: 116 BRAVO5
15:00:58 T:3316  NOTICE: 117 BRAVO6
15:00:58 T:3316  NOTICE: 118 BRAVO7




Here is the xml:

Code:
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="" stop="20140517070000">
<title lang="en">The Karate Kid Part III</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140517070000" stop="20140517093000">
<title lang="en">The Karate Kid</title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>



Here is the code:

Code:
#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('allchannels.url')
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))

if os.path.exists(profilePath):
  profilePath = profilePath + 'source.db'
  con = database.connect(profilePath)
  cur = con.cursor()
  cur.execute('CREATE TABLE programs(id TEXT, channel TEXT, title TEXT, start_date TIMESTAMP, end_date TIMESTAMP, description TEXT)')
  con.commit()
  con.close
  tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()

  profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
  profilePath = profilePath + 'source.db'
  con = database.connect(profilePath)
  cur = con.cursor()
  channels = OrderedDict()

  for elem in tv_elem.getchildren():
    if elem.tag == 'channel':
       channels[elem.attrib['id']] = self.load_channel(elem)
    elif elem.tag == 'programme':
       # get channel
       channel = channels[elem.attrib['programme']]
       channel = channels[elem.attrib['title']]
       # load program in channel
       channel.get_programs().append(self.load_programme(elem))
       for channel_key in channels:
         channel = channels[channel_key]
         display_name = channel.get_display_name()
         print channel.get_display_name()


Can you please help me how i can print the list of format from the start="" and stop="" tags as well from the title tag?
Reply
#2
does anyone know how I can fetch the value from the start="", stop="" and title tags in the xml?
Reply
#3
I've never used elementtree before, but I would have thought that, where your tag is "programme", you could do
Code:
start = elem.get("start")

I could be way off the mark here.

Have a look at the docs for guidance: https://docs.python.org/2/library/xml.et...ttree.html
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#4
Thank you for your advise, well I tried to use the link the one you post but none of the code does work for xbmc python version 2.6.

Do you know how I can print the channels and the title using the elementtree with this code?

Code:
for elem in tv_elem.getchildren():
    if elem.tag == 'channel':
       channels[elem.attrib['id']] = self.load_channel(elem)
    elif elem.tag == 'programme':
       # get channel
       channel = channels[elem.attrib['programme']]
       channel = channels[elem.attrib['title']]
       # load program in channel
       channel.get_programs().append(self.load_programme(elem))
       for channel_key in channels:
         channel = channels[channel_key]
         display_name = channel.get_display_name()
         title_name = get_programs()
         print channel.get_display_name()
         print channel.get_programs()
Reply
#5
What error message do you get?

I've tried on python 2.7 (not in XBMC) and the following worked for me:
Code:
if elem.tag == 'programme':
    print elem.get("start")
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#6
(2014-05-21, 01:00)el_Paraguayo Wrote: What error message do you get?

I've tried on python 2.7 (not in XBMC) and the following worked for me:
Code:
if elem.tag == 'programme':
    print elem.get("start")


There are no error, but please see the xml log: http://xbmclogs.com/show.php?id=206442

The code you gave me it do not work.
Reply
#7
OK. Give me a bit more info, what version of xbmc are you running? Can you post a link to your source code as the code above appears to be just an extract.

As I said, I can't replicate your problem at the moment as if I use the XML extract above and the code from my earlier post, I get the info that you'retrying to extract.

I'm also a bit confused by some of your code. You seem to define "channel" twice:
Code:
# get channel
   channel = channels[elem.attrib['programme']]
   channel = channels[elem.attrib['title']]
but then immediately overwrite that again when you do:
Code:
for channel_key in channels:
    channel = channels[channel_key]

Lastly, I've never used elementtree before so there may be someone else reading this post who can offer some better insight. However, if you post the info above then I'll try to take a look this evening if I have some spare time.

Also, how are you importing elementtree? Have you seen this: http://forum.xbmc.org/showthread.php?tid=173887
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#8
Ok, what I want to do is to search for the tags called display-name, title..etc then I want to extract the strings under the channel tag for per channel. The XBMC version I'm using is 12.2.

The link for my code is: http://pastebin.com/CfM1czbZ
Reply
#9
Can you also give me a full xml file rather than just the extract above.

I won't be able to use your code as it's doing some sql stuff, but I'll just take a look at doing a very simple script that runs from within XBMC to see if I can extract the bits of the xml file you want.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#10
Sure, here it is: http://pastebin.com/hdubBu4C
Reply
#11
OK. I think your problem is that the "programme" tag is nested inside the "channel" tag, but your script is expecting them to be on the same level.

If I do this:
Code:
import xml.etree.ElementTree as ET

## You need to load your xml file here ##
tree = ET.parse(xml)
#########################################

root = tree.getroot()

# Just loop through the channel objects
for elem in [c for c in list(root) if c.tag == "channel"]:

    # Just loop through programmes
    for prog in [p for p in list(elem) if p.tag == "programme"]:

        # Title is in a child element, so we use the "find" method to retrieve it
        # The "start" object is in "programme" so we use "get"
        print "Title: %s\tStart: %s" % (prog.find("title").text, prog.get("start"))

Then this outputs correctly to xbmc log:
Code:
19:17:34 T:140379419752192  NOTICE: Title: The Middle -  The Ditch    Start: 20140520170000
19:17:34 T:140379419752192  NOTICE: Title: The Goonies    Start: 20140520173000
19:17:34 T:140379419752192  NOTICE: Title: Pirates of the Caribbean: On Stranger Tides    Start: 20140520200000
19:17:34 T:140379419752192  NOTICE: Title: The 700 Club    Start: 20140520230000
.....
I'd strongly recommend also looking at the ElementTree docs: https://docs.python.org/2/library/xml.et...ttree.html

This should be enough to get you on your way, but let me know if there's anything else you're stuck with.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#12
Thank you very much for your help el_Paraguayo, I can see it is working right now. Smile
Reply
#13
My pleasure.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply

Logout Mark Read Team Forum Stats Members Help
how to print the list of strings from xml tags0