Re: Fwd: xml to mysql (vice versa ) too

Discussion in 'Python' started by Maric Michaud, Jun 24, 2008.

  1. Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit :
    > can anybody help me in this....
    >
    > -swapna
    >
    > ---------- Forwarded message ----------
    > From: swapna mudavath <>
    > Date: Mon, Jun 23, 2008 at 5:27 PM
    > Subject: xml to mysql (vice versa ) too
    > To:
    >
    >
    > Hi,
    >
    > I need to write a python script to store data which is in XML to MYSQL and
    > even vice versa....
    > what should be the approach?
    > i am able to establish a connection,create tables and insert data .....
    > but how to read an xml file and store in MYSQL....
    > my XML structure is like
    >
    > <list title=" xyz", id = "1",........>
    > <item name=" abc" ,pos="1",........>
    > </item>
    > <item name =" hgdf", pos ="3",......>
    > </item>
    > .
    > ....
    > ...
    > </list>


    This is not valid xml, there is no commas in attribute list in xml.

    > can somebody please help me......i am really confused!!!!!!
    >
    > thanks in advance :)


    You could try with minidom if your xml stream isn't too large, else sax parser
    is to be considered, but minidom is pretty easy to use.

    Give it a try and come back with more specific questions.

    In [82]: from xml.dom.minidom import parseString

    In [83]: xml = """<list title=" xyz" id= "1">
    <item name=" abc" pos="1">
    </item>
    <item name=" hgdf" pos ="3">
    </item>
    </list>"""

    In [89]: def print_nodes(node) :
    print node
    if node.attributes :
    for n, v in node.attributes.items() : print n, v
    for i in node.childNodes : print_nodes(i)
    ....:
    ....:

    In [94]: dom = parseString(xml)

    In [95]: print_nodes(dom)
    <xml.dom.minidom.Document instance at 0xc2b7e8>
    <DOM Element: list at 0xc2b998>
    id 1
    title xyz
    <DOM Text node "
    ">
    <DOM Element: item at 0xc2bd40>
    name abc
    pos 1
    <DOM Text node "
    ">
    <DOM Text node "
    ">
    <DOM Element: item at 0xc30050>
    name hgdf
    pos 3
    <DOM Text node "
    ">
    <DOM Text node "
    ">


    --
    _____________

    Maric Michaud
    Maric Michaud, Jun 24, 2008
    #1
    1. Advertising

  2. > Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit :
    >> can anybody help me in this....
    >>
    >> -swapna
    >>
    >> ---------- Forwarded message ----------
    >> From: swapna mudavath <>
    >> Date: Mon, Jun 23, 2008 at 5:27 PM
    >> Subject: xml to mysql (vice versa ) too
    >> To:
    >>
    >>
    >> Hi,
    >>
    >> I need to write a python script to store data which is in XML to MYSQL and
    >> even vice versa....
    >> what should be the approach?
    >> i am able to establish a connection,create tables and insert data .....
    >> but how to read an xml file and store in MYSQL....
    >> my XML structure is like
    >>
    >> <list title=" xyz", id = "1",........>
    >> <item name=" abc" ,pos="1",........>
    >> </item>
    >> <item name =" hgdf", pos ="3",......>
    >> </item>
    >> .
    >> ....
    >> ...
    >> </list>

    >
    > This is not valid xml, there is no commas in attribute list in xml.


    Yes, so maybe you should tell us where you got this from and if you are in the
    position to change this?


    >> can somebody please help me......i am really confused!!!!!!


    Try lxml.objectify. It lets you work with XML as it if were simple Python objects.

    http://codespeak.net/lxml/objectify.html

    That should allow you to easily extract data from the XML and to generate XML
    from the data you get from MySQL.

    Stefan
    Stefan Behnel, Jun 24, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jimmy
    Replies:
    1
    Views:
    3,148
    Eliyahu Goldin
    Jun 14, 2005
  2. Sam Hwang

    Java Object to XML and vice versa

    Sam Hwang, Apr 24, 2005, in forum: Java
    Replies:
    5
    Views:
    1,003
    iksrazal
    Apr 25, 2005
  3. Zunbeltz Izaola

    from data structure to xml an vice versa

    Zunbeltz Izaola, Dec 12, 2003, in forum: XML
    Replies:
    0
    Views:
    386
    Zunbeltz Izaola
    Dec 12, 2003
  4. Matt Garman
    Replies:
    1
    Views:
    654
    Matt Garman
    Apr 25, 2004
  5. JyotiC

    xml to object and vice versa

    JyotiC, Jul 23, 2008, in forum: Java
    Replies:
    10
    Views:
    617
    JyotiC
    Jul 30, 2008
Loading...

Share This Page