使用RestFul Web服务中的xml
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用RestFul Web服务中的xml相关的知识,希望对你有一定的参考价值。
ElementTree is provided as part of the standard Python libs. ElementTree is pure python, and cElementTree is the faster C implementation:Here's an example usage, where I'm consuming xml from a RESTful web service:
# Try to use the C implementation first, falling back to python try: from xml.etree import cElementTree as ElementTree except ImportError, e: from xml.etree import ElementTree ########## def find(*args, **kwargs): """Find a book in the collection specified""" search_args = [('access_key', api_key),] if not is_valid_collection(kwargs['collection']): return None kwargs.pop('collection') for key in kwargs: # Only the first keword is honored if kwargs[key]: search_args.append(('index1', key)) search_args.append(('value1', kwargs[key])) break url = urllib.basejoin(api_url, '%s.xml' % 'books') data = urllib.urlencode(search_args) req = urllib2.urlopen(url, data) rdata = [] chunk = 'xx' while chunk: chunk = req.read() if chunk: rdata.append(chunk) tree = ElementTree.fromstring(''.join(rdata)) results = [] for i, elem in enumerate(tree.getiterator('BookData')): results.append( {'isbn': elem.get('isbn'), 'isbn13': elem.get('isbn13'), 'title': elem.find('Title').text, 'author': elem.find('AuthorsText').text, 'publisher': elem.find('PublisherText').text,} ) return results
以上是关于使用RestFul Web服务中的xml的主要内容,如果未能解决你的问题,请参考以下文章
将 Spring Boot RESTful Web 服务部署到 Tomcat 时仍然缺少 Web.xml
使用 Apache Shiro 的 Restful Web 服务身份验证和授权