Python/django 中的 xml.dom.minidom Document() 输出内存位置

发布于 2024-07-24 20:00:30 字数 1992 浏览 8 评论 0原文

我正在同时学习Python和django。 我正在尝试创建一个 xml 文档以从视图返回一些 XML。 我目前正在使用 django 开发服务器,并且我不断在我的视图中获取这些信息,而不是我尝试创建的文档。

这是我的代码

    from django.http import HttpResponse
    from mypoject.myapp.models import Username
    from django.core import serializers
    from xml.dom.minidom import Document
    import datetime


    def authenticate(request, username):
        if request.method == "GET":

            #Try to get the username
            try:
                checkUser = Username.objects.get(username__exact = username)
                user = userCheck.get(username__exact = username)
                userXML = serializers.serialize("xml", checkUser)

            except Username.DoesNotExist:
                #return XML with status "Failed"
                return HttpResponse(xml, mimetype="text/xml")       
            except:
                #return XML with status "Failed"

                xmlFailed = Document()

                meta = xmlFailed.createElement("meta")
                xmlFailed.appendChild(meta)

                status = xmlFailed.createElement("status")
                meta.appendChild(status)
                statusText = xmlFailed.createTextNode("Failed")
                status.appendChild(statusText)

                message = xmlFailed.createElement("message")
                meta.appendChild(message)

                totalRecords = xmlFailed.createElement("totalRecords")
                meta.appendChild(totalRecords)

                executionTime = xmlFailed.createElement("executionTime")
                meta.appendChild(executionTime)

                return HttpResponse(xmlFailed, mimetype="text/xml")
            else:
                #return happy XML code with status "Success"

这是当我在浏览器中查看时屏幕上显示的内容...

<xml.dom.minidom.Document instance at 0x993192c>

如果我注释掉 Document() 创建,该内容就会消失。 所以我想我只是需要它而不是吐出信息。 我一直在寻找,但找不到一个明确的答案,这让我相信我错过了一些明显的东西。

谢谢你的帮助!

I'm learning Python and django at the same time. I'm trying to create an xml document to return some XML from a view. I'm using the django development server at the moment and I keep getting this information spitting out in my views instead of the document I tried to create.

Here's my code

    from django.http import HttpResponse
    from mypoject.myapp.models import Username
    from django.core import serializers
    from xml.dom.minidom import Document
    import datetime


    def authenticate(request, username):
        if request.method == "GET":

            #Try to get the username
            try:
                checkUser = Username.objects.get(username__exact = username)
                user = userCheck.get(username__exact = username)
                userXML = serializers.serialize("xml", checkUser)

            except Username.DoesNotExist:
                #return XML with status "Failed"
                return HttpResponse(xml, mimetype="text/xml")       
            except:
                #return XML with status "Failed"

                xmlFailed = Document()

                meta = xmlFailed.createElement("meta")
                xmlFailed.appendChild(meta)

                status = xmlFailed.createElement("status")
                meta.appendChild(status)
                statusText = xmlFailed.createTextNode("Failed")
                status.appendChild(statusText)

                message = xmlFailed.createElement("message")
                meta.appendChild(message)

                totalRecords = xmlFailed.createElement("totalRecords")
                meta.appendChild(totalRecords)

                executionTime = xmlFailed.createElement("executionTime")
                meta.appendChild(executionTime)

                return HttpResponse(xmlFailed, mimetype="text/xml")
            else:
                #return happy XML code with status "Success"

And here's what's going to the screen when I view it in my browser...

<xml.dom.minidom.Document instance at 0x993192c>

If I comment out the Document() creation that goes away. So I'm think I just need it to not spit out the information. I've been searching all over and I can't find a strait answer which leads me to believe I'm missing something blatantly obvious.

Thanks for any help!

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

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

发布评论

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

评论(1

放血 2024-07-31 20:00:30

您需要调用 xmlFailed.toxml() 或类似方法才能从对象中获取 XML —— 看起来这不是您正在做的事情(在您没有显示的代码中)我们)。

You'll need to call xmlFailed.toxml() or the like in order to get XML out of your object -- looks like that's not what you're doing (in the code you didn't show us).

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