Domanda

I'm working on a Python script to read xml data from a web server. I also want to store the data in an sqlite database, but I still have no idea how to parse the xml data prior to storing the data.

Here is the current code:

import xbmc
import xbmcgui
import xbmcaddon
import urllib2

ADDON = xbmcaddon.Addon(id = 'script.myaddon')

class MyScript(xbmcgui.WindowXML):

def __new__(cls):
         return super(MyScript, cls).__new__(cls, 'script-menu.xml', ADDON.getAddonInfo('path'))

def onInit(self):
   url = ADDON.getSetting('ontv.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 = lite.connect(profilePath)
      cur = con.cursor()
      cur.execute('CREATE TABLE IF NOT EXISTS data (channels, programme_title, programme_time, description, logo_url)')
   cur.close()

return data
cSetVisible(self,4201,True)

Here's the settings.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<settings>
    <category label="30101">
        <setting id="source" label="30101" type="labelenum" default="YouSee.tv"
                 values="YouSee.tv|XMLTV|ONTV.dk" />

        <setting id="youseetv.category" label="30102" type="labelenum" default="Danske"
                 values="Danske|Filmkanaler|Grundpakken|Mellempakken|Fuldpakken|Nordiske|Engelske|Tyske|L​atinske|Slaviske|Tyrkiske|Kurdiske|Arabiske|Asiatiske"
                 visible="eq(-1,0)" />
        <setting id="program.background.enabled" label="30107" type="bool" default="true" visible="eq(-2,0)"/>
        <setting id="english.enabled" value="true"/>
        <setting id="french.enabled" value="false"/>
        <setting id="xmltv.file" label="30103" type="file" visible="eq(-3,1)" />
        <setting id="xmltv.logo.folder" label="30116" type="folder" visible="eq(-4,1)"/>
        <setting id="ontv.url" label="30117" type="text" visible="eq(-5,2)" default="http://ontv.dk/xmltv/c81e728d9d4c2f636f067f89cc14862c"/>
    </category>
</settings>

I'm reading the xml data from this website: http://ontv.dk/xmltv/c81e728d9d4c2f636f067f89cc14862c

I know how to read the xml data with response.read(), but I still have no idea how to parse the xml data to store in the database.

How I can parse the xml data and store it in the database?

È stato utile?

Soluzione

As other mentioned I also recommend lxml

If you want to look through the xml structure and make it in a very pythonic way look at
lxml objectify
or
untangle

With this libraries you should get the data out of the XML. Maybe keep in mind this is not the best and fastest approach for larger XML files.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top