0

I have done following code in python to get the response of query in XML stored in eXist-db. I get the value but the problem is type 'instant' as given in the output below. Here's my code:

from eulexistdb import db
class TestExist:
    def __init__(self):
        self.db = db.ExistDB("http://localhost:8899/exist/")   

    def get_res(self,query):
        #result = list()
        res = self.db.executeQuery(query)
        hits = self.db.getHits(res)        
        for i in range(hits):
            print self.db.retrieve(res,i)
            print type(self.db.retrieve(res,i))
xquery = '''
let $x:= doc("/db/sample/books.xml")
return $x/bookstore/book/price/text()'''
a = TestExist()
a.get_res(xquery)

Now the query works fine and result too get printed as:

30.00
<type 'instance'>
29.99
<type 'instance'>
49.99
<type 'instance'>
39.95
<type 'instance'>

What I want is to return the value appended in list 'result'. I tried type conversion but failed. How do I achieve this?

Joe Wicentowski
  • 5,159
  • 16
  • 26
Mahadeva
  • 1,584
  • 4
  • 23
  • 56

1 Answers1

2

That's strange - the docs seem to say that the db.retrieve function should return a string, but apparently it does not. In any case, since the print statement gets a useful string out of it, one of these should work:

result.append(str(self.db.retrieve(res,i)))

or

result.append(repr(self.db.retrieve(res,i)))

Just uncomment the #result = list() line, and add one of the above into the for loop.

Brionius
  • 13,858
  • 3
  • 38
  • 49
  • casting to str() worked. So silly I didnt thought of it. I wanted float Value out of it. But the repr() didnt worked as resulted output like ['', '', '', ''] – Mahadeva Aug 14 '13 at 13:30
  • Great - glad it worked. If you feel it solved your problem, clicking the "accept" checkmark next to the answer will be helpful for other users to know that it worked. – Brionius Aug 14 '13 at 17:13