Cherry Py - 在 Python 中以 XML 形式返回输出

发布于 2024-10-26 15:29:16 字数 766 浏览 1 评论 0原文

我的目的是在 Google App Engine 中部署网络服务。我使用 CherryPy,因为我发现它很容易理解。

import sys
sys.path.insert(0,'cherrypy.zip')

import cherrypy
from cherrypy import expose

class Converter:
    @expose
    def index(self):
        return "Hello World!"

    @expose
    def fahr_to_celc(self, degrees):
        temp = (float(degrees) - 32) * 5 / 9
        return "%.01f" % temp

    @expose
    def celc_to_fahr(self, degrees):
        temp = float(degrees) * 9 / 5 + 32
        return "%.01f" % temp

cherrypy.quickstart(Converter())

我想知道如何以 XML 格式返回输出,就像

<?xml version="1.0" encoding="UTF-8"?> 
<root>
    <answer>Hello World!</answer>    
</root>

我是 Python 初学者一样。请帮助我。

哈里哈兰

My intention is to deploy a web service in Google App Engine. I am using CherryPy as I found it very easy to understand.

import sys
sys.path.insert(0,'cherrypy.zip')

import cherrypy
from cherrypy import expose

class Converter:
    @expose
    def index(self):
        return "Hello World!"

    @expose
    def fahr_to_celc(self, degrees):
        temp = (float(degrees) - 32) * 5 / 9
        return "%.01f" % temp

    @expose
    def celc_to_fahr(self, degrees):
        temp = float(degrees) * 9 / 5 + 32
        return "%.01f" % temp

cherrypy.quickstart(Converter())

I would like to know, how to return the output in XML format, like

<?xml version="1.0" encoding="UTF-8"?> 
<root>
    <answer>Hello World!</answer>    
</root>

I am a beginner in Python. Kindly help me.

Hariharan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-11-02 15:29:16

我有类似的问题。我的解决方案是使用 xml elementtree。就像

....
#elementtree is stored in weird places... This catches most of em
try:
    import xml.etree.ElementTree as ET # in python >=2.5
except ImportError:
    try:
            import cElementTree as ET # effbot's C module
        except ImportError:
        try:
            import elementtree.ElementTree as ET # effbot's pure Python module
            except ImportError:
                    try:
                        import lxml.etree as ET # ElementTree API using libxml2
                    except ImportError:
                        import warnings
                        warnings.warn("could not import ElementTree "
                                "(http://effbot.org/zone/element-index.htm)")

def build_xml_tree(answer_txt=""):
    if not len(resources):
        return ""
    root = ET.Element("root")
    answer = ET.SubElement(root, "answer")
    answer.text = answer_txt
    xml_string = ET.tostring(root)
    return rxml_string

然后从你的函数中调用 build_xml_tree

I had a similar issue. My solution was to use xml elementtree. It was something like

....
#elementtree is stored in weird places... This catches most of em
try:
    import xml.etree.ElementTree as ET # in python >=2.5
except ImportError:
    try:
            import cElementTree as ET # effbot's C module
        except ImportError:
        try:
            import elementtree.ElementTree as ET # effbot's pure Python module
            except ImportError:
                    try:
                        import lxml.etree as ET # ElementTree API using libxml2
                    except ImportError:
                        import warnings
                        warnings.warn("could not import ElementTree "
                                "(http://effbot.org/zone/element-index.htm)")

def build_xml_tree(answer_txt=""):
    if not len(resources):
        return ""
    root = ET.Element("root")
    answer = ET.SubElement(root, "answer")
    answer.text = answer_txt
    xml_string = ET.tostring(root)
    return rxml_string

Then call build_xml_tree from your function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文