how to read an lxml.objectify.ObjectifiedElement object with django-piston?
I am trying to make a django-piston API which reads another API and presents it in a different format (reads an XML-only API and presents it in JSON). But I am having trouble, because the way I am doing it I just an empty string.
Here is my handler:
class MyHandler(BaseHandler):
methods_allowed = ('GET')
def read(self, request, my_argument):
my_node = get_node(my_argument)
return my_node
The get_node function is just a function which gets the object from the other API, a开发者_StackOverflow中文版nd that is an lxml object:
In [27]: type(my_node)
Out[27]: <type 'lxml.objectify.ObjectifiedElement'>
In my urls.py I have:
from django.conf.urls.defaults import *
from piston.resource import Resource
from api.handlers import MyHandler
class CsrfExemptResource( Resource ):
def __init__( self, handler, authentication = None ):
super( CsrfExemptResource, self ).__init__( handler, authentication )
self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True )
my_resource = CsrfExemptResource( MyHandler )
urlpatterns = patterns( '',
url( r'^api/(?P<my_argument>.*)/$', my_resource ),
)
And when I visit that URL (with the right argument, which given directly to get_node gives a correct object), I just get an empty string.
How to make it work?
It depends on what you need from that lxml.objectify.ObjectifiedElement. If you want to get the text content from that element you can access it via
my_node.text
If you want an attribute value you can access it via:
my_node.attrib['nameofattribute']
精彩评论