2

Here is my code:

#!/usr/bin/python

import psycopg2
import sys
from lxml import etree
def main():

    #Define our connection string
    conn_string = ("host=host dbname=lal user=user password=pass")


    # get a connection, if a connect cannot be made an exception will be raised here
    conn = psycopg2.connect(conn_string)

    # conn.cursor will return a cursor object
    cursor = conn.cursor()
    print "Connected!\n"

    # Open file


    parser = etree.parse("XML/epg.xml")
    for row in parser:
        print row

        postgres = ('INSERT INTO epg_live (channel_id, program, start, duration) VALUES (%s, %s, %s, %s)', (row, row, row, row))
        cursor.execute(parser,postgres)
        cursor.commit()
        print "Gotovo!"

if __name__ == "__main__":
    main()

Can you help me with parsing XML file to string and insert into table in posgresql. When I run script i get errors like:

File "./xml.py", line 32, in <module>
    main()
  File "./xml.py", line 22, in main
    parser = etree.parse("XML/epg.xml")
  File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
  File "parser.pxi", line 1533, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82287)
  File "parser.pxi", line 1562, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:82580)
  File "parser.pxi", line 1462, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:81619)
  File "parser.pxi", line 1002, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:78528)
  File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
  File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
  File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: epg line 2 and item, line 26, column 10

My XML is fine it looks like:

<item><program>        Program 3   
</program><start>            Start   20130918 15:00:00 
</start><duration>            Duration   04:30:00 
</duration><title>                  Title Nujna seja Odbora za finance in monetarno politiko   
</title></item>

Can you help me with some solution for python, thx guys for reading this post.

3
  • Run xmllint command on your XML file to check there is no error Commented Sep 20, 2013 at 8:37
  • I can parse your xml example without any problem, you have to check your xml Commented Sep 20, 2013 at 8:44
  • But how can i read my xml file into string and send it as string on postgresql? Commented Sep 20, 2013 at 8:51

1 Answer 1

2

You can read xml into parameters and send to PostgreSQL like this:

root = etree.parse("XML/epg.xml")
for i in root.findall("item"):
    p = [i.find(n).text for n in ("program", "start", "duration")]
    # now you get list with values of parameters

    postgres = ('INSERT INTO epg_live (program, start, duration) VALUES (%s, %s, %s)', p)
    cursor.execute(parser,postgres)
    cursor.commit()

don't know where to get channel_id parameter

7
  • I get this error:File "./xml.py", line 22 p = [i.find(n).text for n in ("program", "start", "duration", "title")] ^ IndentationError: unindent does not match any outer indentation level Commented Sep 20, 2013 at 9:02
  • check indentation in your program, you have to use all tabs OR all spaces Commented Sep 20, 2013 at 9:08
  • Thx for advice, i've checked all and now get this:File "./proba.py", line 24 p = [i.find(n).text ^ IndentationError: expected an indented block Commented Sep 20, 2013 at 9:16
  • Sry but I'm new to python and i wanna get better. Commented Sep 20, 2013 at 9:17
  • Roman Pekar, thx for solution when I run my code: Traceback (most recent call last): File "./proba.py", line 22, in <module> root = etree.parse("XML/epg.xml") Thsi is problem with XML file? File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204) File "parser.pxi", line 1533, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82287) Commented Sep 20, 2013 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.