How to insert data in data base by xml file

113 views
Skip to first unread message

Sai Harsh Tondomker

unread,
Jul 1, 2015, 10:01:19 AM7/1/15
to web...@googlegroups.com

My project is to make simple quizzing site.

I have made data base in web2py.

with Question, options and Correct answer.

The data will be xml file. From XML file I need to take data and store in data base

Please help me to solve the problem.

Waiting for your response.

Best Regards
T.Sai Harsh

Anthony

unread,
Jul 1, 2015, 10:26:17 AM7/1/15
to web...@googlegroups.com, saihar...@iiits.in
You can parse the XML with something like ElementTree, lxml, or Beautiful Soup. Then you would have to write some code to extract the data for individual records (which will depend on the structure of the XML) and use the DAL to insert each record.

Anthony

Sai Harsh Tondomker

unread,
Jul 3, 2015, 12:00:22 PM7/3/15
to web...@googlegroups.com, saihar...@iiits.in
Thanks for replying.
Could you please give me one example.

Waiting for your response

Anthony

unread,
Jul 3, 2015, 5:43:55 PM7/3/15
to web...@googlegroups.com, saihar...@iiits.in
All of the effort will be in parsing the XML, so you should check the documentation of the various XML libraries and ask questions in their forums (or on Stack Overflow). The details will depend on the structure of your XML. Ultimately, you want to loop over the records in the XML, and for each record, build a dictionary where the keys are the DAL table field names and the values are the record values you want to insert. Once you have such a dictionary, the DAL insert for a single record is simple:

db.mytable.insert(**record_dictionary)

Anthony

Johann Spies

unread,
Jul 6, 2015, 3:41:13 AM7/6/15
to web...@googlegroups.com
On 3 July 2015 at 23:43, Anthony <abas...@gmail.com> wrote:
All of the effort will be in parsing the XML, so you should check the documentation of the various XML libraries and ask questions in their forums (or on Stack Overflow). The details will depend on the structure of your XML. Ultimately, you want to loop over the records in the XML, and for each record, build a dictionary where the keys are the DAL table field names and the values are the record values you want to insert. Once you have such a dictionary, the DAL insert for a single record is simple:

db.mytable.insert(**record_dictionary)

Another approach is to import the XML-data directly into a PostgreSQL table with and id (primary key) field and another field with the type XML.  This can be queried using XPATH-like syntax in PostgreSQL (from web2py using exexutesql) and if you want to import that into 'normal' tables, this can be done within PostgreSQL using the above-mentioned xpath-based queries.

Both ways have a learning curve if you are not used to parsing XML-data.

Regards
Johann

Sai Harsh Tondomker

unread,
Jul 6, 2015, 11:22:11 AM7/6/15
to web...@googlegroups.com
Thanks for reply.
Could you please give me one example and where I can get enough matter to write the code.


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/ccWa_xekecQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Jul 6, 2015, 11:46:45 AM7/6/15
to web...@googlegroups.com, saihar...@iiits.in
On Monday, July 6, 2015 at 11:22:11 AM UTC-4, Sai Harsh Tondomker wrote:
Thanks for reply.
Could you please give me one example and where I can get enough matter to write the code.

Note, this is not web2py specific.  As suggested, check the various XML libraries and seek help via SO or their forums. Once you have the XML parsed, the web2py database insert is simple.

Dave S

unread,
Jul 6, 2015, 4:26:13 PM7/6/15
to web...@googlegroups.com


On Monday, July 6, 2015 at 8:22:11 AM UTC-7, Sai Harsh Tondomker wrote:
Thanks for reply.
Could you please give me one example and where I can get enough matter to write the code.



Here's a little fragment of stuff I'm doing (not with Web2Py) using Beautiful Soup:


  f = codecs.open("path/myfilename",
                 encoding
="ISO8859-1", mode="r")
  soup
= BeautifulSoup(f)
 
 
for x in soup.find_all("td", class_="element-title"):
     
if not x.contents[0]:
       
print "blowing up on %s" % x.name
     
while not isinstance(x.contents[0], NavigableString):
       x
= x.contents[0]


Note that I'm actually parsing HTML ("element-title" shows up in <TD> nodes), but the general approach is the same.  If you go with Beautiful Soup, I think you'll find their tutorial is quite easy to follow.  After all, I was able to follow it, and come up with the above.  The code that follows this is essentially taking apart the text string I find in my target node, so most of the rest of my code is Python string ops (x.split(), etc).

I'm not familiar with the other suggestions, except that I think Beautiful Soup builds on lxml.

/dps

Carla Raquel

unread,
Jul 7, 2015, 12:24:51 PM7/7/15
to web...@googlegroups.com
This is how I dealt with my xml files:

doc = xml.dom.minidom.parse("...my_xml.xml")// Here you parse the xml file

for node in doc.getElementsByTagName("recipe")://Here you read in a cycle the tags which contain  numerous attributes each

        t
=node.getAttribute("name")//Each attribute is stored as a string in a variable that you can add to the db
        d
=node.getAttribute("creator")


try:
    db
.Recipe.update_or_insert(Name=t,Creator=d)//each time you cycle through each node you are adding the name and creator to the db if both aren't already there
except:
   
pass

Hope it helped.
Reply all
Reply to author
Forward
0 new messages